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"
29 /* Allow use of deprecated function tevent_loop_allow_nesting() */
30 #define TEVENT_DEPRECATED
34 #include "lib/tdb_wrap/tdb_wrap.h"
35 #include "lib/util/dlinklist.h"
36 #include "lib/util/debug.h"
37 #include "lib/util/substitute.h"
38 #include "lib/util/time.h"
40 #include "ctdb_logging.h"
41 #include "ctdb_version.h"
42 #include "ctdb_private.h"
43 #include "ctdb_client.h"
45 #include "common/cmdline.h"
46 #include "common/rb_tree.h"
47 #include "common/system.h"
48 #include "common/common.h"
50 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
51 #define ERR_NONODE 21 /* node does not exist */
52 #define ERR_DISNODE 22 /* node is disconnected */
54 static void usage(void);
61 const char *machineseparator;
64 int printemptyrecords;
71 #define LONGTIMEOUT options.timelimit*10
73 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
74 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
76 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
78 return (tv2->tv_sec - tv->tv_sec) +
79 (tv2->tv_usec - tv->tv_usec)*1.0e-6;
82 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
84 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
88 /* Like printf(3) but substitute for separator in format */
89 static int printm(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
90 static int printm(const char *format, ...)
94 size_t len = strlen(format);
95 char new_format[len+1];
97 strcpy(new_format, format);
99 if (options.machineseparator[0] != ':') {
100 all_string_sub(new_format,
101 ":", options.machineseparator, len + 1);
104 va_start(ap, format);
105 ret = vprintf(new_format, ap);
111 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
112 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
113 "Out of memory in " __location__ )); \
117 static uint32_t getpnn(struct ctdb_context *ctdb)
119 if ((options.pnn == CTDB_BROADCAST_ALL) ||
120 (options.pnn == CTDB_MULTICAST)) {
122 ("Cannot get PNN for node %u\n", options.pnn));
126 if (options.pnn == CTDB_CURRENT_NODE) {
127 return ctdb_get_pnn(ctdb);
133 static void assert_single_node_only(void)
135 if ((options.pnn == CTDB_BROADCAST_ALL) ||
136 (options.pnn == CTDB_MULTICAST)) {
138 ("This control can not be applied to multiple PNNs\n"));
143 static void assert_current_node_only(struct ctdb_context *ctdb)
145 if (options.pnn != ctdb_get_pnn(ctdb)) {
147 ("This control can only be applied to the current node\n"));
152 /* Pretty print the flags to a static buffer in human-readable format.
153 * This never returns NULL!
155 static const char *pretty_print_flags(uint32_t flags)
158 static const struct {
162 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
163 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
164 { NODE_FLAGS_BANNED, "BANNED" },
165 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
166 { NODE_FLAGS_DELETED, "DELETED" },
167 { NODE_FLAGS_STOPPED, "STOPPED" },
168 { NODE_FLAGS_INACTIVE, "INACTIVE" },
170 static char flags_str[512]; /* Big enough to contain all flag names */
173 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
174 if (flags & flag_names[j].flag) {
175 if (flags_str[0] == '\0') {
176 (void) strcpy(flags_str, flag_names[j].name);
178 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
179 (void) strncat(flags_str, flag_names[j].name,
180 sizeof(flags_str)-1);
184 if (flags_str[0] == '\0') {
185 (void) strcpy(flags_str, "OK");
191 static int h2i(char h)
193 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
194 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
198 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
201 TDB_DATA key = {NULL, 0};
204 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
209 key.dptr = talloc_size(mem_ctx, key.dsize);
211 for (i=0; i < len/2; i++) {
212 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
217 static TDB_DATA strtodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
221 if (!strncmp(str, "0x", 2)) {
222 key = hextodata(mem_ctx, str + 2, len - 2);
224 key.dptr = talloc_memdup(mem_ctx, str, len);
231 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
232 * that are disconnected or deleted. If dd_ok is true those nodes are
233 * included in the output list of nodes. If dd_ok is false, those
234 * nodes are filtered from the "all" case and cause an error if
235 * explicitly specified.
237 static bool parse_nodestring(struct ctdb_context *ctdb,
239 const char * nodestring,
240 uint32_t current_pnn,
245 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
248 struct ctdb_node_map_old *nodemap;
253 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
255 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
256 talloc_free(tmp_ctx);
260 if (nodestring != NULL) {
261 *nodes = talloc_array(mem_ctx, uint32_t, 0);
262 if (*nodes == NULL) {
268 if (strcmp(nodestring, "all") == 0) {
269 *pnn_mode = CTDB_BROADCAST_ALL;
272 for (i = 0; i < nodemap->num; i++) {
273 if ((nodemap->nodes[i].flags &
274 (NODE_FLAGS_DISCONNECTED |
275 NODE_FLAGS_DELETED)) && !dd_ok) {
278 *nodes = talloc_realloc(mem_ctx, *nodes,
280 if (*nodes == NULL) {
290 ns = talloc_strdup(tmp_ctx, nodestring);
291 tok = strtok(ns, ",");
292 while (tok != NULL) {
295 i = (uint32_t)strtoul(tok, &endptr, 0);
296 if (i == 0 && tok == endptr) {
298 ("Invalid node %s\n", tok));
299 talloc_free(tmp_ctx);
302 if (i >= nodemap->num) {
303 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
304 talloc_free(tmp_ctx);
307 if ((nodemap->nodes[i].flags &
308 (NODE_FLAGS_DISCONNECTED |
309 NODE_FLAGS_DELETED)) && !dd_ok) {
310 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
311 talloc_free(tmp_ctx);
314 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
315 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
316 talloc_free(tmp_ctx);
320 *nodes = talloc_realloc(mem_ctx, *nodes,
322 if (*nodes == NULL) {
329 tok = strtok(NULL, ",");
334 *pnn_mode = (*nodes)[0];
336 *pnn_mode = CTDB_MULTICAST;
340 /* default - no nodes specified */
341 *nodes = talloc_array(mem_ctx, uint32_t, 1);
342 if (*nodes == NULL) {
345 *pnn_mode = CTDB_CURRENT_NODE;
347 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
352 talloc_free(tmp_ctx);
356 talloc_free(tmp_ctx);
361 check if a database exists
363 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
364 uint32_t *dbid, const char **dbname, uint8_t *flags)
367 struct ctdb_dbid_map_old *dbmap=NULL;
368 bool dbid_given = false, found = false;
370 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
371 const char *name = NULL;
373 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
375 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
379 if (strncmp(dbarg, "0x", 2) == 0) {
380 id = strtoul(dbarg, NULL, 0);
384 for(i=0; i<dbmap->num; i++) {
386 if (id == dbmap->dbs[i].db_id) {
391 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, tmp_ctx, &name);
393 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].db_id));
397 if (strcmp(name, dbarg) == 0) {
398 id = dbmap->dbs[i].db_id;
405 if (found && dbid_given && dbname != NULL) {
406 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, tmp_ctx, &name);
408 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].db_id));
415 if (dbid) *dbid = id;
416 if (dbname) *dbname = talloc_strdup(ctdb, name);
417 if (flags) *flags = dbmap->dbs[i].flags;
419 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
423 talloc_free(tmp_ctx);
428 see if a process exists
430 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
438 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
439 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
443 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
445 printf("%u:%u exists\n", pnn, pid);
447 printf("%u:%u does not exist\n", pnn, pid);
453 display statistics structure
455 static void show_statistics(struct ctdb_statistics *s, int show_header)
457 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
459 const char *prefix=NULL;
461 int tmp, days, hours, minutes, seconds;
466 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
467 STATISTICS_FIELD(num_clients),
468 STATISTICS_FIELD(frozen),
469 STATISTICS_FIELD(recovering),
470 STATISTICS_FIELD(num_recoveries),
471 STATISTICS_FIELD(client_packets_sent),
472 STATISTICS_FIELD(client_packets_recv),
473 STATISTICS_FIELD(node_packets_sent),
474 STATISTICS_FIELD(node_packets_recv),
475 STATISTICS_FIELD(keepalive_packets_sent),
476 STATISTICS_FIELD(keepalive_packets_recv),
477 STATISTICS_FIELD(node.req_call),
478 STATISTICS_FIELD(node.reply_call),
479 STATISTICS_FIELD(node.req_dmaster),
480 STATISTICS_FIELD(node.reply_dmaster),
481 STATISTICS_FIELD(node.reply_error),
482 STATISTICS_FIELD(node.req_message),
483 STATISTICS_FIELD(node.req_control),
484 STATISTICS_FIELD(node.reply_control),
485 STATISTICS_FIELD(client.req_call),
486 STATISTICS_FIELD(client.req_message),
487 STATISTICS_FIELD(client.req_control),
488 STATISTICS_FIELD(timeouts.call),
489 STATISTICS_FIELD(timeouts.control),
490 STATISTICS_FIELD(timeouts.traverse),
491 STATISTICS_FIELD(locks.num_calls),
492 STATISTICS_FIELD(locks.num_current),
493 STATISTICS_FIELD(locks.num_pending),
494 STATISTICS_FIELD(locks.num_failed),
495 STATISTICS_FIELD(total_calls),
496 STATISTICS_FIELD(pending_calls),
497 STATISTICS_FIELD(childwrite_calls),
498 STATISTICS_FIELD(pending_childwrite_calls),
499 STATISTICS_FIELD(memory_used),
500 STATISTICS_FIELD(max_hop_count),
501 STATISTICS_FIELD(total_ro_delegations),
502 STATISTICS_FIELD(total_ro_revokes),
505 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
514 if (options.machinereadable){
516 printm("CTDB version:");
517 printm("Current time of statistics:");
518 printm("Statistics collected since:");
519 for (i=0;i<ARRAY_SIZE(fields);i++) {
520 printm("%s:", fields[i].name);
522 printm("num_reclock_ctdbd_latency:");
523 printm("min_reclock_ctdbd_latency:");
524 printm("avg_reclock_ctdbd_latency:");
525 printm("max_reclock_ctdbd_latency:");
527 printm("num_reclock_recd_latency:");
528 printm("min_reclock_recd_latency:");
529 printm("avg_reclock_recd_latency:");
530 printm("max_reclock_recd_latency:");
532 printm("num_call_latency:");
533 printm("min_call_latency:");
534 printm("avg_call_latency:");
535 printm("max_call_latency:");
537 printm("num_lockwait_latency:");
538 printm("min_lockwait_latency:");
539 printm("avg_lockwait_latency:");
540 printm("max_lockwait_latency:");
542 printm("num_childwrite_latency:");
543 printm("min_childwrite_latency:");
544 printm("avg_childwrite_latency:");
545 printm("max_childwrite_latency:");
548 printm("%d:", CTDB_PROTOCOL);
549 printm("%d:", (int)s->statistics_current_time.tv_sec);
550 printm("%d:", (int)s->statistics_start_time.tv_sec);
551 for (i=0;i<ARRAY_SIZE(fields);i++) {
552 printm("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
554 printm("%d:", s->reclock.ctdbd.num);
555 printm("%.6f:", s->reclock.ctdbd.min);
556 printm("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
557 printm("%.6f:", s->reclock.ctdbd.max);
559 printm("%d:", s->reclock.recd.num);
560 printm("%.6f:", s->reclock.recd.min);
561 printm("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
562 printm("%.6f:", s->reclock.recd.max);
564 printm("%d:", s->call_latency.num);
565 printm("%.6f:", s->call_latency.min);
566 printm("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
567 printm("%.6f:", s->call_latency.max);
569 printm("%d:", s->childwrite_latency.num);
570 printm("%.6f:", s->childwrite_latency.min);
571 printm("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
572 printm("%.6f:", s->childwrite_latency.max);
575 printf("CTDB version %u\n", CTDB_PROTOCOL);
576 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
577 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
579 for (i=0;i<ARRAY_SIZE(fields);i++) {
580 if (strchr(fields[i].name, '.')) {
581 preflen = strcspn(fields[i].name, ".")+1;
582 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
583 prefix = fields[i].name;
584 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
589 printf(" %*s%-22s%*s%10u\n",
591 fields[i].name+preflen,
593 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
595 printf(" hop_count_buckets:");
596 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
597 printf(" %d", s->hop_count_bucket[i]);
600 printf(" lock_buckets:");
601 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
602 printf(" %d", s->locks.buckets[i]);
605 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);
607 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);
609 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);
611 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);
612 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);
615 talloc_free(tmp_ctx);
619 display remote ctdb statistics combined from all nodes
621 static int control_statistics_all(struct ctdb_context *ctdb)
624 struct ctdb_statistics statistics;
628 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
629 CTDB_NO_MEMORY(ctdb, nodes);
631 ZERO_STRUCT(statistics);
633 for (i=0;i<num_nodes;i++) {
634 struct ctdb_statistics s1;
636 uint32_t *v1 = (uint32_t *)&s1;
637 uint32_t *v2 = (uint32_t *)&statistics;
639 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
640 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
642 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
645 for (j=0;j<num_ints;j++) {
648 statistics.max_hop_count =
649 MAX(statistics.max_hop_count, s1.max_hop_count);
650 statistics.call_latency.max =
651 MAX(statistics.call_latency.max, s1.call_latency.max);
654 printf("Gathered statistics for %u nodes\n", num_nodes);
655 show_statistics(&statistics, 1);
660 display remote ctdb statistics
662 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
665 struct ctdb_statistics statistics;
667 if (options.pnn == CTDB_BROADCAST_ALL) {
668 return control_statistics_all(ctdb);
671 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
673 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
676 show_statistics(&statistics, 1);
682 reset remote ctdb statistics
684 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
688 ret = ctdb_statistics_reset(ctdb, options.pnn);
690 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
698 display remote ctdb rolling statistics
700 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
703 struct ctdb_statistics_list_old *stats;
704 int i, num_records = -1;
706 assert_single_node_only();
709 num_records = atoi(argv[0]) - 1;
712 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
714 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
717 for (i=0;i<stats->num;i++) {
718 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
721 show_statistics(&stats->stats[i], i==0);
722 if (i == num_records) {
731 display remote ctdb db statistics
733 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
735 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
736 struct ctdb_db_statistics_old *dbstat;
746 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
750 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
752 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
753 talloc_free(tmp_ctx);
757 printf("DB Statistics: %s\n", argv[0]);
758 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
759 dbstat->db_ro_delegations);
760 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
761 dbstat->db_ro_delegations);
762 printf(" %s\n", "locks");
763 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
764 dbstat->locks.num_calls);
765 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
766 dbstat->locks.num_failed);
767 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
768 dbstat->locks.num_current);
769 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
770 dbstat->locks.num_pending);
771 printf(" %s", "hop_count_buckets:");
772 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
773 printf(" %d", dbstat->hop_count_bucket[i]);
776 printf(" %s", "lock_buckets:");
777 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
778 printf(" %d", dbstat->locks.buckets[i]);
781 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
782 "locks_latency MIN/AVG/MAX",
783 dbstat->locks.latency.min,
784 (dbstat->locks.latency.num ?
785 dbstat->locks.latency.total /dbstat->locks.latency.num :
787 dbstat->locks.latency.max,
788 dbstat->locks.latency.num);
789 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
790 "vacuum_latency MIN/AVG/MAX",
791 dbstat->vacuum.latency.min,
792 (dbstat->vacuum.latency.num ?
793 dbstat->vacuum.latency.total /dbstat->vacuum.latency.num :
795 dbstat->vacuum.latency.max,
796 dbstat->vacuum.latency.num);
798 for (i=0; i<dbstat->num_hot_keys; i++) {
799 if (dbstat->hot_keys[i].count > 0) {
803 dbstat->num_hot_keys = num_hot_keys;
805 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
806 for (i = 0; i < dbstat->num_hot_keys; i++) {
808 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
809 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
810 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
815 talloc_free(tmp_ctx);
820 display uptime of remote node
822 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
825 struct ctdb_uptime *uptime = NULL;
826 int tmp, days, hours, minutes, seconds;
828 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
830 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
834 if (options.machinereadable){
835 printm(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
836 printm(":%u:%u:%u:%lf\n",
837 (unsigned int)uptime->current_time.tv_sec,
838 (unsigned int)uptime->ctdbd_start_time.tv_sec,
839 (unsigned int)uptime->last_recovery_finished.tv_sec,
840 timeval_delta(&uptime->last_recovery_finished,
841 &uptime->last_recovery_started)
846 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
848 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
856 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
858 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
866 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
868 printf("Duration of last recovery/failover: %lf seconds\n",
869 timeval_delta(&uptime->last_recovery_finished,
870 &uptime->last_recovery_started));
876 show the PNN of the current node
878 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
882 mypnn = getpnn(ctdb);
884 printf("PNN:%d\n", mypnn);
889 static struct ctdb_node_map_old *read_nodes_file(TALLOC_CTX *mem_ctx)
891 const char *nodes_list;
893 /* read the nodes file */
894 nodes_list = getenv("CTDB_NODES");
895 if (nodes_list == NULL) {
896 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
897 getenv("CTDB_BASE"));
898 if (nodes_list == NULL) {
899 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
904 return ctdb_read_nodes_file(mem_ctx, nodes_list);
908 show the PNN of the current node
909 discover the pnn by loading the nodes file and try to bind to all
910 addresses one at a time until the ip address is found.
912 static int find_node_xpnn(void)
914 TALLOC_CTX *mem_ctx = talloc_new(NULL);
915 struct ctdb_node_map_old *node_map;
918 node_map = read_nodes_file(mem_ctx);
919 if (node_map == NULL) {
920 talloc_free(mem_ctx);
924 for (i = 0; i < node_map->num; i++) {
925 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
928 if (ctdb_sys_have_ip(&node_map->nodes[i].addr)) {
929 pnn = node_map->nodes[i].pnn;
930 talloc_free(mem_ctx);
935 printf("Failed to detect which PNN this node is\n");
936 talloc_free(mem_ctx);
940 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
944 assert_single_node_only();
946 pnn = find_node_xpnn();
951 printf("PNN:%d\n", pnn);
955 /* Helpers for ctdb status
957 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
959 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
963 if (node->flags == 0) {
964 struct ctdb_control_get_ifaces *ifaces;
966 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
967 tmp_ctx, &ifaces) == 0) {
968 for (j=0; j < ifaces->num; j++) {
969 if (ifaces->ifaces[j].link_state != 0) {
977 talloc_free(tmp_ctx);
982 static void control_status_header_machine(void)
984 printm(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
985 ":Inactive:PartiallyOnline:ThisNode:\n");
988 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
989 struct ctdb_node_and_flags *node)
991 printm(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
992 ctdb_addr_to_str(&node->addr),
993 !!(node->flags&NODE_FLAGS_DISCONNECTED),
994 !!(node->flags&NODE_FLAGS_BANNED),
995 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
996 !!(node->flags&NODE_FLAGS_UNHEALTHY),
997 !!(node->flags&NODE_FLAGS_STOPPED),
998 !!(node->flags&NODE_FLAGS_INACTIVE),
999 is_partially_online(ctdb, node) ? 1 : 0,
1000 (node->pnn == mypnn)?'Y':'N');
1005 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
1006 struct ctdb_node_and_flags *node)
1008 printf("pnn:%d %-16s %s%s\n", node->pnn,
1009 ctdb_addr_to_str(&node->addr),
1010 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
1011 node->pnn == mypnn?" (THIS NODE)":"");
1017 display remote ctdb status
1019 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
1021 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1023 struct ctdb_vnn_map *vnnmap=NULL;
1024 struct ctdb_node_map_old *nodemap=NULL;
1025 uint32_t recmode, recmaster, mypnn;
1026 int num_deleted_nodes = 0;
1029 mypnn = getpnn(ctdb);
1031 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1033 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1034 talloc_free(tmp_ctx);
1038 if (options.machinereadable) {
1039 control_status_header_machine();
1040 for (i=0;i<nodemap->num;i++) {
1041 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1044 (void) control_status_1_machine(ctdb, mypnn,
1045 &nodemap->nodes[i]);
1047 talloc_free(tmp_ctx);
1051 for (i=0; i<nodemap->num; i++) {
1052 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1053 num_deleted_nodes++;
1056 if (num_deleted_nodes == 0) {
1057 printf("Number of nodes:%d\n", nodemap->num);
1059 printf("Number of nodes:%d (including %d deleted nodes)\n",
1060 nodemap->num, num_deleted_nodes);
1062 for(i=0;i<nodemap->num;i++){
1063 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1066 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1069 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1071 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1072 talloc_free(tmp_ctx);
1075 if (vnnmap->generation == INVALID_GENERATION) {
1076 printf("Generation:INVALID\n");
1078 printf("Generation:%d\n",vnnmap->generation);
1080 printf("Size:%d\n",vnnmap->size);
1081 for(i=0;i<vnnmap->size;i++){
1082 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1085 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1087 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1088 talloc_free(tmp_ctx);
1091 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1093 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1095 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1096 talloc_free(tmp_ctx);
1099 printf("Recovery master:%d\n",recmaster);
1101 talloc_free(tmp_ctx);
1105 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1107 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1109 struct ctdb_node_map_old *nodemap=NULL;
1111 uint32_t pnn_mode, mypnn;
1117 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1118 options.pnn, true, &nodes, &pnn_mode)) {
1122 if (options.machinereadable) {
1123 control_status_header_machine();
1124 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1125 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1128 mypnn = getpnn(ctdb);
1130 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1132 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1133 talloc_free(tmp_ctx);
1139 for (i = 0; i < talloc_array_length(nodes); i++) {
1140 if (options.machinereadable) {
1141 ret |= control_status_1_machine(ctdb, mypnn,
1142 &nodemap->nodes[nodes[i]]);
1144 ret |= control_status_1_human(ctdb, mypnn,
1145 &nodemap->nodes[nodes[i]]);
1149 talloc_free(tmp_ctx);
1153 static struct ctdb_node_map_old *read_natgw_nodes_file(struct ctdb_context *ctdb,
1154 TALLOC_CTX *mem_ctx)
1156 const char *natgw_list;
1157 struct ctdb_node_map_old *natgw_nodes = NULL;
1159 natgw_list = getenv("CTDB_NATGW_NODES");
1160 if (natgw_list == NULL) {
1161 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1162 getenv("CTDB_BASE"));
1163 if (natgw_list == NULL) {
1164 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1168 /* The PNNs/flags will be junk but they're not used */
1169 natgw_nodes = ctdb_read_nodes_file(mem_ctx, natgw_list);
1170 if (natgw_nodes == NULL) {
1172 ("Failed to load natgw node list '%s'\n", natgw_list));
1178 /* talloc off the existing nodemap... */
1179 static struct ctdb_node_map_old *talloc_nodemap(struct ctdb_node_map_old *nodemap)
1181 return talloc_zero_size(nodemap,
1182 offsetof(struct ctdb_node_map_old, nodes) +
1183 nodemap->num * sizeof(struct ctdb_node_and_flags));
1186 static struct ctdb_node_map_old *
1187 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1188 struct ctdb_node_map_old *nodemap,
1189 struct ctdb_node_map_old *natgw_nodes)
1192 struct ctdb_node_map_old *ret;
1194 ret = talloc_nodemap(nodemap);
1195 CTDB_NO_MEMORY_NULL(ctdb, ret);
1199 for (i = 0; i < nodemap->num; i++) {
1200 for(j = 0; j < natgw_nodes->num ; j++) {
1201 if (nodemap->nodes[j].flags & NODE_FLAGS_DELETED) {
1204 if (ctdb_same_ip(&natgw_nodes->nodes[j].addr,
1205 &nodemap->nodes[i].addr)) {
1207 ret->nodes[ret->num] = nodemap->nodes[i];
1217 static struct ctdb_node_map_old *
1218 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1219 struct ctdb_node_map_old *nodemap,
1220 uint32_t required_capabilities,
1224 uint32_t capabilities;
1225 struct ctdb_node_map_old *ret;
1227 ret = talloc_nodemap(nodemap);
1228 CTDB_NO_MEMORY_NULL(ctdb, ret);
1232 for (i = 0; i < nodemap->num; i++) {
1235 /* Disconnected nodes have no capabilities! */
1236 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1240 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1241 nodemap->nodes[i].pnn,
1244 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1245 nodemap->nodes[i].pnn));
1249 if (!(capabilities & required_capabilities)) {
1253 ret->nodes[ret->num] = nodemap->nodes[i];
1263 static struct ctdb_node_map_old *
1264 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1265 struct ctdb_node_map_old *nodemap,
1266 uint32_t flags_mask)
1269 struct ctdb_node_map_old *ret;
1271 ret = talloc_nodemap(nodemap);
1272 CTDB_NO_MEMORY_NULL(ctdb, ret);
1276 for (i = 0; i < nodemap->num; i++) {
1277 if (nodemap->nodes[i].flags & flags_mask) {
1281 ret->nodes[ret->num] = nodemap->nodes[i];
1289 display the list of nodes belonging to this natgw configuration
1291 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1293 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1295 struct ctdb_node_map_old *natgw_nodes = NULL;
1296 struct ctdb_node_map_old *orig_nodemap=NULL;
1297 struct ctdb_node_map_old *nodemap;
1298 uint32_t mypnn, pnn;
1301 /* When we have some nodes that could be the NATGW, make a
1302 * series of attempts to find the first node that doesn't have
1303 * certain status flags set.
1305 uint32_t exclude_flags[] = {
1306 /* Look for a nice healthy node */
1307 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1308 /* If not found, an UNHEALTHY/BANNED node will do */
1309 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1310 /* If not found, a STOPPED node will do */
1311 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1315 /* read the natgw nodes file into a linked list */
1316 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1317 if (natgw_nodes == NULL) {
1322 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1323 tmp_ctx, &orig_nodemap);
1325 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1326 talloc_free(tmp_ctx);
1330 /* Get a nodemap that includes only the nodes in the NATGW
1332 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1333 if (nodemap == NULL) {
1338 ret = 2; /* matches ENOENT */
1341 /* For each flag mask... */
1342 for (i = 0; exclude_flags[i] != 0; i++) {
1343 /* ... get a nodemap that excludes nodes with with
1344 * masked flags... */
1345 struct ctdb_node_map_old *t =
1346 filter_nodemap_by_flags(ctdb, nodemap,
1354 /* ... and find the first node with the NATGW
1356 struct ctdb_node_map_old *n;
1357 n = filter_nodemap_by_capabilities(ctdb, t,
1367 pnn = n->nodes[0].pnn;
1368 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1375 if (options.machinereadable) {
1376 printm(":Node:IP:\n");
1377 printm(":%d:%s:\n", pnn, ip);
1379 printf("%d %s\n", pnn, ip);
1382 /* print the pruned list of nodes belonging to this natgw list */
1383 mypnn = getpnn(ctdb);
1384 if (options.machinereadable) {
1385 control_status_header_machine();
1387 printf("Number of nodes:%d\n", nodemap->num);
1389 for(i=0;i<nodemap->num;i++){
1390 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1393 if (options.machinereadable) {
1394 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1396 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1401 talloc_free(tmp_ctx);
1406 display the status of the scripts for monitoring (or other events)
1408 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1409 enum ctdb_event type)
1411 struct ctdb_script_list_old *script_status;
1414 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1416 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1420 if (script_status == NULL) {
1421 if (!options.machinereadable) {
1422 printf("%s cycle never run\n",
1423 ctdb_eventscript_call_names[type]);
1428 if (!options.machinereadable) {
1430 for (i=0; i<script_status->num_scripts; i++) {
1431 if (script_status->scripts[i].status != -ENOEXEC) {
1435 printf("%d scripts were executed last %s cycle\n",
1437 ctdb_eventscript_call_names[type]);
1439 for (i=0; i<script_status->num_scripts; i++) {
1440 const char *status = NULL;
1442 /* The ETIME status is ignored for certain events.
1443 * In that case the status is 0, but endtime is not set.
1445 if (script_status->scripts[i].status == 0 &&
1446 timeval_is_zero(&script_status->scripts[i].finished)) {
1447 script_status->scripts[i].status = -ETIME;
1450 switch (script_status->scripts[i].status) {
1452 status = "TIMEDOUT";
1455 status = "DISABLED";
1461 if (script_status->scripts[i].status > 0)
1465 if (options.machinereadable) {
1466 printm(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1467 ctdb_eventscript_call_names[type],
1468 script_status->scripts[i].name,
1469 script_status->scripts[i].status,
1471 (long)script_status->scripts[i].start.tv_sec,
1472 (long)script_status->scripts[i].start.tv_usec,
1473 (long)script_status->scripts[i].finished.tv_sec,
1474 (long)script_status->scripts[i].finished.tv_usec,
1475 script_status->scripts[i].output);
1479 printf("%-20s Status:%s ",
1480 script_status->scripts[i].name, status);
1482 /* Some other error, eg from stat. */
1483 printf("%-20s Status:CANNOT RUN (%s)",
1484 script_status->scripts[i].name,
1485 strerror(-script_status->scripts[i].status));
1487 if (script_status->scripts[i].status >= 0) {
1488 printf("Duration:%.3lf ",
1489 timeval_delta(&script_status->scripts[i].finished,
1490 &script_status->scripts[i].start));
1492 if (script_status->scripts[i].status != -ENOEXEC) {
1494 ctime(&script_status->scripts[i].start.tv_sec));
1495 if (script_status->scripts[i].status != 0) {
1496 printf(" OUTPUT:%s\n",
1497 script_status->scripts[i].output);
1507 static int control_scriptstatus(struct ctdb_context *ctdb,
1508 int argc, const char **argv)
1511 enum ctdb_event type, min, max;
1515 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1520 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1524 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1525 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1531 if (type == CTDB_EVENT_MAX) {
1532 if (strcmp(arg, "all") == 0) {
1534 max = CTDB_EVENT_MAX;
1536 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1541 if (options.machinereadable) {
1542 printm(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1545 for (type = min; type < max; type++) {
1546 ret = control_one_scriptstatus(ctdb, type);
1556 enable an eventscript
1558 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1566 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1568 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1576 disable an eventscript
1578 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1586 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1588 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1596 display the pnn of the recovery master
1598 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1603 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1605 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1608 printf("%d\n",recmaster);
1614 add a tickle to a public address
1616 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1618 struct ctdb_connection t;
1622 assert_single_node_only();
1628 if (parse_ip_port(argv[0], &t.src) == 0) {
1629 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1632 if (parse_ip_port(argv[1], &t.dst) == 0) {
1633 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1637 data.dptr = (uint8_t *)&t;
1638 data.dsize = sizeof(t);
1640 /* tell all nodes about this tcp connection */
1641 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1642 0, data, ctdb, NULL, NULL, NULL, NULL);
1644 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1653 delete a tickle from a node
1655 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1657 struct ctdb_connection t;
1661 assert_single_node_only();
1667 if (parse_ip_port(argv[0], &t.src) == 0) {
1668 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1671 if (parse_ip_port(argv[1], &t.dst) == 0) {
1672 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1676 data.dptr = (uint8_t *)&t;
1677 data.dsize = sizeof(t);
1679 /* tell all nodes about this tcp connection */
1680 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1681 0, data, ctdb, NULL, NULL, NULL, NULL);
1683 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1692 get a list of all tickles for this pnn
1694 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1696 struct ctdb_control_tcp_tickle_list *list;
1697 ctdb_sock_addr addr;
1701 assert_single_node_only();
1708 port = atoi(argv[1]);
1711 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1712 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1716 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1718 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1722 if (options.machinereadable){
1723 printm(":source ip:port:destination ip:port:\n");
1724 for (i=0;i<list->tickles.num;i++) {
1725 if (port && port != ntohs(list->tickles.connections[i].dst.ip.sin_port)) {
1728 printm(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src), ntohs(list->tickles.connections[i].src.ip.sin_port));
1729 printm(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst), ntohs(list->tickles.connections[i].dst.ip.sin_port));
1732 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1733 printf("Num tickles:%u\n", list->tickles.num);
1734 for (i=0;i<list->tickles.num;i++) {
1735 if (port && port != ntohs(list->tickles.connections[i].dst.ip.sin_port)) {
1738 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src), ntohs(list->tickles.connections[i].src.ip.sin_port));
1739 printf("DST: %s:%u\n", ctdb_addr_to_str(&list->tickles.connections[i].dst), ntohs(list->tickles.connections[i].dst.ip.sin_port));
1749 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1751 struct ctdb_public_ip_list_old *ips;
1752 struct ctdb_public_ip ip;
1755 uint32_t disable_time;
1757 struct ctdb_node_map_old *nodemap=NULL;
1758 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1761 data.dptr = (uint8_t*)&disable_time;
1762 data.dsize = sizeof(disable_time);
1763 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1765 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1771 /* read the public ip list from the node */
1772 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1774 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1775 talloc_free(tmp_ctx);
1779 for (i=0;i<ips->num;i++) {
1780 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1785 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1786 pnn, ctdb_addr_to_str(addr)));
1787 talloc_free(tmp_ctx);
1794 data.dptr = (uint8_t *)&ip;
1795 data.dsize = sizeof(ip);
1797 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1799 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1800 talloc_free(tmp_ctx);
1804 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1805 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1812 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1813 talloc_free(tmp_ctx);
1817 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1819 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1820 talloc_free(tmp_ctx);
1824 /* update the recovery daemon so it now knows to expect the new
1825 node assignment for this ip.
1827 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1829 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1833 talloc_free(tmp_ctx);
1839 * scans all other nodes and returns a pnn for another node that can host this
1843 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1845 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1846 struct ctdb_public_ip_list_old *ips;
1847 struct ctdb_node_map_old *nodemap=NULL;
1851 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1853 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1854 talloc_free(tmp_ctx);
1858 for(i=0;i<nodemap->num;i++){
1859 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1862 if (nodemap->nodes[i].pnn == options.pnn) {
1866 /* read the public ip list from this node */
1867 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1869 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1873 for (j=0;j<ips->num;j++) {
1874 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1875 pnn = nodemap->nodes[i].pnn;
1876 talloc_free(tmp_ctx);
1883 talloc_free(tmp_ctx);
1887 /* If pnn is -1 then try to find a node to move IP to... */
1888 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1890 bool pnn_specified = (pnn == -1 ? false : true);
1893 while (retries < 5) {
1894 if (!pnn_specified) {
1895 pnn = find_other_host_for_public_ip(ctdb, addr);
1900 ("Trying to move public IP to node %u\n", pnn));
1903 if (move_ip(ctdb, addr, pnn) == 0) {
1916 move/failover an ip address to a specific node
1918 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1921 ctdb_sock_addr addr;
1923 assert_single_node_only();
1930 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1931 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1936 if (sscanf(argv[1], "%u", &pnn) != 1) {
1937 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1941 if (!try_moveip(ctdb, &addr, pnn)) {
1942 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1949 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1953 data.dptr = (uint8_t *)&pnn;
1954 data.dsize = sizeof(uint32_t);
1955 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1957 ("Failed to send message to force node %u to be a rebalancing target\n",
1967 rebalance a node by setting it to allow failback and triggering a
1970 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1972 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1977 assert_single_node_only();
1983 /* Determine the nodes where IPs need to be reloaded */
1984 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1985 options.pnn, true, &nodes, &pnn_mode)) {
1990 for (i = 0; i < talloc_array_length(nodes); i++) {
1991 if (!rebalance_node(ctdb, nodes[i])) {
1997 talloc_free(tmp_ctx);
2001 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
2003 struct ctdb_public_ip ip;
2006 uint32_t disable_time;
2008 struct ctdb_node_map_old *nodemap=NULL;
2009 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2012 data.dptr = (uint8_t*)&disable_time;
2013 data.dsize = sizeof(disable_time);
2014 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
2016 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
2023 data.dptr = (uint8_t *)&ip;
2024 data.dsize = sizeof(ip);
2026 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
2028 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2029 talloc_free(tmp_ctx);
2033 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2034 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
2041 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
2042 talloc_free(tmp_ctx);
2046 talloc_free(tmp_ctx);
2051 release an ip form all nodes and have it re-assigned by recd
2053 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2055 ctdb_sock_addr addr;
2057 assert_single_node_only();
2064 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2065 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2069 if (rebalance_ip(ctdb, &addr) != 0) {
2070 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2077 static int getips_store_callback(void *param, void *data)
2079 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2080 struct ctdb_public_ip_list_old *ips = param;
2084 ips->ips[i].pnn = node_ip->pnn;
2085 ips->ips[i].addr = node_ip->addr;
2089 static int getips_count_callback(void *param, void *data)
2091 uint32_t *count = param;
2098 static uint32_t *ip_key(ctdb_sock_addr *ip)
2100 static uint32_t key[IP_KEYLEN];
2102 bzero(key, sizeof(key));
2104 switch (ip->sa.sa_family) {
2106 key[0] = ip->ip.sin_addr.s_addr;
2109 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2117 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2124 static void *add_ip_callback(void *parm, void *data)
2130 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_public_ip_list_old **ips)
2132 struct ctdb_public_ip_list_old *tmp_ips;
2133 struct ctdb_node_map_old *nodemap=NULL;
2134 trbt_tree_t *ip_tree;
2138 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2140 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2144 ip_tree = trbt_create(tmp_ctx, 0);
2146 for(i=0;i<nodemap->num;i++){
2147 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2150 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2154 /* read the public ip list from this node */
2155 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2157 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2161 for (j=0; j<tmp_ips->num;j++) {
2162 struct ctdb_public_ip *node_ip;
2164 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2165 node_ip->pnn = tmp_ips->ips[j].pnn;
2166 node_ip->addr = tmp_ips->ips[j].addr;
2168 trbt_insertarray32_callback(ip_tree,
2169 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2173 talloc_free(tmp_ips);
2178 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2180 len = offsetof(struct ctdb_public_ip_list_old, ips) +
2181 count*sizeof(struct ctdb_public_ip);
2182 tmp_ips = talloc_zero_size(tmp_ctx, len);
2183 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2191 static void ctdb_every_second(struct tevent_context *ev,
2192 struct tevent_timer *te,
2193 struct timeval t, void *p)
2195 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2197 tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
2198 ctdb_every_second, ctdb);
2201 struct srvid_reply_handler_data {
2205 const char *srvid_str;
2208 static void srvid_broadcast_reply_handler(uint64_t srvid, TDB_DATA data,
2211 struct srvid_reply_handler_data *d =
2212 (struct srvid_reply_handler_data *)private_data;
2216 if (data.dsize != sizeof(ret)) {
2217 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2221 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2222 ret = *(int32_t *)data.dptr;
2225 ("%s failed with result %d\n", d->srvid_str, ret));
2229 if (!d->wait_for_all) {
2234 /* Wait for all replies */
2236 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2237 if (d->nodes[i] == ret) {
2239 ("%s reply received from node %u\n",
2240 d->srvid_str, ret));
2243 if (d->nodes[i] != -1) {
2244 /* Found a node that hasn't yet replied */
2250 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2251 * or replies from all connected nodes. arg is the data argument to
2252 * pass in the srvid_request structure - pass 0 if this isn't needed.
2254 static int srvid_broadcast(struct ctdb_context *ctdb,
2255 uint64_t srvid, uint32_t *arg,
2256 const char *srvid_str, bool wait_for_all)
2261 uint64_t reply_srvid;
2262 struct ctdb_srvid_message request;
2263 struct srvid_request_data request_data;
2264 struct srvid_reply_handler_data reply_data;
2267 ZERO_STRUCT(request);
2269 /* Time ticks to enable timeouts to be processed */
2270 tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
2271 ctdb_every_second, ctdb);
2273 pnn = ctdb_get_pnn(ctdb);
2274 reply_srvid = getpid();
2278 request.srvid = reply_srvid;
2280 data.dptr = (uint8_t *)&request;
2281 data.dsize = sizeof(request);
2283 request_data.pnn = pnn;
2284 request_data.srvid = reply_srvid;
2285 request_data.data = *arg;
2287 data.dptr = (uint8_t *)&request_data;
2288 data.dsize = sizeof(request_data);
2291 /* Register message port for reply from recovery master */
2292 ctdb_client_set_message_handler(ctdb, reply_srvid,
2293 srvid_broadcast_reply_handler,
2296 reply_data.wait_for_all = wait_for_all;
2297 reply_data.nodes = NULL;
2298 reply_data.srvid_str = srvid_str;
2301 reply_data.done = false;
2304 struct ctdb_node_map_old *nodemap;
2306 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2307 CTDB_CURRENT_NODE, ctdb, &nodemap);
2310 ("Unable to get nodemap from current node, try again\n"));
2315 if (reply_data.nodes != NULL) {
2316 talloc_free(reply_data.nodes);
2318 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2321 talloc_free(nodemap);
2324 /* Send to all connected nodes. Only recmaster replies */
2325 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2328 /* This can only happen if the socket is closed and
2329 * there's no way to recover from that, so don't try
2333 ("Failed to send %s request to connected nodes\n",
2338 tv = timeval_current();
2339 /* This loop terminates the reply is received */
2340 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2341 tevent_loop_once(ctdb->ev);
2344 if (!reply_data.done) {
2346 ("Still waiting for confirmation of %s\n", srvid_str));
2351 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2353 talloc_free(reply_data.nodes);
2358 static int ipreallocate(struct ctdb_context *ctdb)
2360 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2361 "IP reallocation", false);
2365 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2367 return ipreallocate(ctdb);
2371 add a public ip address to a node
2373 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2376 int len, retries = 0;
2378 ctdb_sock_addr addr;
2379 struct ctdb_control_ip_iface *pub;
2380 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2381 struct ctdb_public_ip_list_old *ips;
2385 talloc_free(tmp_ctx);
2389 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2390 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2391 talloc_free(tmp_ctx);
2395 /* read the public ip list from the node */
2396 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2398 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2399 talloc_free(tmp_ctx);
2402 for (i=0;i<ips->num;i++) {
2403 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2404 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2411 /* Dont timeout. This command waits for an ip reallocation
2412 which sometimes can take wuite a while if there has
2413 been a recent recovery
2417 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2418 pub = talloc_size(tmp_ctx, len);
2419 CTDB_NO_MEMORY(ctdb, pub);
2423 pub->len = strlen(argv[1])+1;
2424 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2427 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2429 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2433 } while (retries < 5 && ret != 0);
2435 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2436 talloc_free(tmp_ctx);
2440 if (rebalance_node(ctdb, options.pnn) != 0) {
2441 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2445 talloc_free(tmp_ctx);
2450 add a public ip address to a node
2452 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2454 ctdb_sock_addr addr;
2461 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2462 printf("Badly formed ip : %s\n", argv[0]);
2466 iface = ctdb_sys_find_ifname(&addr);
2467 if (iface == NULL) {
2468 printf("Failed to get interface name for ip: %s", argv[0]);
2472 printf("IP on interface %s\n", iface);
2479 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2481 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2483 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2484 struct ctdb_node_map_old *nodemap=NULL;
2485 struct ctdb_public_ip_list_old *ips;
2488 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2490 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2494 /* remove it from the nodes that are not hosting the ip currently */
2495 for(i=0;i<nodemap->num;i++){
2496 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2499 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2501 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2505 for (j=0;j<ips->num;j++) {
2506 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2514 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2518 options.pnn = nodemap->nodes[i].pnn;
2519 control_delip(ctdb, argc, argv);
2523 /* remove it from every node (also the one hosting it) */
2524 for(i=0;i<nodemap->num;i++){
2525 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2528 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2530 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2534 for (j=0;j<ips->num;j++) {
2535 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2543 options.pnn = nodemap->nodes[i].pnn;
2544 control_delip(ctdb, argc, argv);
2547 talloc_free(tmp_ctx);
2552 delete a public ip address from a node
2554 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2557 ctdb_sock_addr addr;
2558 struct ctdb_control_ip_iface pub;
2559 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2560 struct ctdb_public_ip_list_old *ips;
2563 talloc_free(tmp_ctx);
2567 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2568 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2572 if (options.pnn == CTDB_BROADCAST_ALL) {
2573 return control_delip_all(ctdb, argc, argv, &addr);
2580 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2582 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2583 talloc_free(tmp_ctx);
2587 for (i=0;i<ips->num;i++) {
2588 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2594 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2595 ctdb_addr_to_str(&addr)));
2596 talloc_free(tmp_ctx);
2600 /* This is an optimisation. If this node is hosting the IP
2601 * then try to move it somewhere else without invoking a full
2602 * takeover run. We don't care if this doesn't work!
2604 if (ips->ips[i].pnn == options.pnn) {
2605 (void) try_moveip(ctdb, &addr, -1);
2608 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2610 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2611 talloc_free(tmp_ctx);
2615 talloc_free(tmp_ctx);
2619 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2620 int argc, const char **argv)
2622 struct ctdb_connection *killtcp;
2623 int max_entries, current, i;
2624 struct timeval timeout;
2625 char line[128], src[128], dst[128];
2628 struct client_async_data *async_data;
2629 struct ctdb_client_control_state *state;
2639 while (!feof(stdin)) {
2640 if (fgets(line, sizeof(line), stdin) == NULL) {
2644 /* Silently skip empty lines */
2645 if (line[0] == '\n') {
2649 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2650 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2652 talloc_free(killtcp);
2656 if (current >= max_entries) {
2657 max_entries += 1024;
2658 killtcp = talloc_realloc(ctdb, killtcp,
2659 struct ctdb_connection,
2661 CTDB_NO_MEMORY(ctdb, killtcp);
2664 if (!parse_ip_port(src, &killtcp[current].src)) {
2665 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2667 talloc_free(killtcp);
2671 if (!parse_ip_port(dst, &killtcp[current].dst)) {
2672 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2674 talloc_free(killtcp);
2681 async_data = talloc_zero(ctdb, struct client_async_data);
2682 if (async_data == NULL) {
2683 talloc_free(killtcp);
2687 for (i = 0; i < current; i++) {
2689 data.dsize = sizeof(struct ctdb_connection);
2690 data.dptr = (unsigned char *)&killtcp[i];
2692 timeout = TIMELIMIT();
2693 state = ctdb_control_send(ctdb, options.pnn, 0,
2694 CTDB_CONTROL_KILL_TCP, 0, data,
2695 async_data, &timeout, NULL);
2697 if (state == NULL) {
2699 ("Failed to call async killtcp control to node %u\n",
2701 talloc_free(killtcp);
2705 ctdb_client_async_add(async_data, state);
2708 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2709 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2710 talloc_free(killtcp);
2714 talloc_free(killtcp);
2720 kill a tcp connection
2722 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2725 struct ctdb_connection killtcp;
2727 assert_single_node_only();
2730 return kill_tcp_from_file(ctdb, argc, argv);
2737 if (!parse_ip_port(argv[0], &killtcp.src)) {
2738 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2742 if (!parse_ip_port(argv[1], &killtcp.dst)) {
2743 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2747 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2749 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2760 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2763 ctdb_sock_addr addr;
2765 assert_single_node_only();
2771 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2772 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2776 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2778 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2786 register a server id
2788 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2791 struct ctdb_client_id server_id;
2797 server_id.pnn = strtoul(argv[0], NULL, 0);
2798 server_id.type = strtoul(argv[1], NULL, 0);
2799 server_id.server_id = strtoul(argv[2], NULL, 0);
2801 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2803 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2806 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2812 unregister a server id
2814 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2817 struct ctdb_client_id server_id;
2823 server_id.pnn = strtoul(argv[0], NULL, 0);
2824 server_id.type = strtoul(argv[1], NULL, 0);
2825 server_id.server_id = strtoul(argv[2], NULL, 0);
2827 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2829 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2836 check if a server id exists
2838 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2840 uint32_t status = 0;
2842 struct ctdb_client_id server_id;
2848 server_id.pnn = strtoul(argv[0], NULL, 0);
2849 server_id.type = strtoul(argv[1], NULL, 0);
2850 server_id.server_id = strtoul(argv[2], NULL, 0);
2852 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2854 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2859 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2861 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2867 get a list of all server ids that are registered on a node
2869 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2872 struct ctdb_client_id_list_old *server_ids;
2874 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2876 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2880 for (i=0; i<server_ids->num; i++) {
2881 printf("Server id %d:%d:%d\n",
2882 server_ids->server_ids[i].pnn,
2883 server_ids->server_ids[i].type,
2884 server_ids->server_ids[i].server_id);
2891 check if a server id exists
2893 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2895 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2901 talloc_free(tmp_ctx);
2905 ids = talloc_array(tmp_ctx, uint64_t, argc);
2906 result = talloc_array(tmp_ctx, uint8_t, argc);
2908 for (i = 0; i < argc; i++) {
2909 ids[i] = strtoull(argv[i], NULL, 0);
2912 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2913 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2915 talloc_free(tmp_ctx);
2919 for (i=0; i < argc; i++) {
2920 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2921 result[i] ? "exists" : "does not exist");
2924 talloc_free(tmp_ctx);
2929 send a tcp tickle ack
2931 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2934 ctdb_sock_addr src, dst;
2940 if (!parse_ip_port(argv[0], &src)) {
2941 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2945 if (!parse_ip_port(argv[1], &dst)) {
2946 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2950 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2954 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2961 display public ip status
2963 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2966 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2967 struct ctdb_public_ip_list_old *ips;
2969 if (argc == 1 && strcmp(argv[0], "all") == 0) {
2970 options.pnn = CTDB_BROADCAST_ALL;
2973 if (options.pnn == CTDB_BROADCAST_ALL) {
2974 /* read the list of public ips from all nodes */
2975 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2977 /* read the public ip list from this node */
2978 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2981 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2982 talloc_free(tmp_ctx);
2986 if (options.machinereadable){
2987 printm(":Public IP:Node:");
2988 if (options.verbose){
2989 printm("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2993 if (options.pnn == CTDB_BROADCAST_ALL) {
2994 printf("Public IPs on ALL nodes\n");
2996 printf("Public IPs on node %u\n", options.pnn);
3000 for (i=1;i<=ips->num;i++) {
3001 struct ctdb_control_public_ip_info *info = NULL;
3003 char *aciface = NULL;
3004 char *avifaces = NULL;
3005 char *cifaces = NULL;
3007 if (options.pnn == CTDB_BROADCAST_ALL) {
3008 pnn = ips->ips[ips->num-i].pnn;
3014 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
3015 &ips->ips[ips->num-i].addr, &info);
3022 for (j=0; j < info->num; j++) {
3023 if (cifaces == NULL) {
3024 cifaces = talloc_strdup(info,
3025 info->ifaces[j].name);
3027 cifaces = talloc_asprintf_append(cifaces,
3029 info->ifaces[j].name);
3032 if (info->active_idx == j) {
3033 aciface = info->ifaces[j].name;
3036 if (info->ifaces[j].link_state == 0) {
3040 if (avifaces == NULL) {
3041 avifaces = talloc_strdup(info, info->ifaces[j].name);
3043 avifaces = talloc_asprintf_append(avifaces,
3045 info->ifaces[j].name);
3050 if (options.machinereadable){
3052 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3053 ips->ips[ips->num-i].pnn);
3054 if (options.verbose){
3057 avifaces?avifaces:"",
3058 cifaces?cifaces:"");
3062 if (options.verbose) {
3063 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3064 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3065 ips->ips[ips->num-i].pnn,
3067 avifaces?avifaces:"",
3068 cifaces?cifaces:"");
3071 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3072 ips->ips[ips->num-i].pnn);
3078 talloc_free(tmp_ctx);
3085 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3088 ctdb_sock_addr addr;
3089 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3090 struct ctdb_control_public_ip_info *info;
3093 talloc_free(tmp_ctx);
3097 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3098 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3102 /* read the public ip info from this node */
3103 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3104 tmp_ctx, &addr, &info);
3106 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3107 argv[0], options.pnn));
3108 talloc_free(tmp_ctx);
3112 printf("Public IP[%s] info on node %u\n",
3113 ctdb_addr_to_str(&info->ip.addr),
3116 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3117 ctdb_addr_to_str(&info->ip.addr),
3118 info->ip.pnn, info->num);
3120 for (i=0; i<info->num; i++) {
3121 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3123 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3124 i+1, info->ifaces[i].name,
3125 info->ifaces[i].link_state?"up":"down",
3126 (unsigned int)info->ifaces[i].references,
3127 (i==info->active_idx)?" (active)":"");
3130 talloc_free(tmp_ctx);
3135 display interfaces status
3137 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3139 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3141 struct ctdb_control_get_ifaces *ifaces;
3144 /* read the public ip list from this node */
3145 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3147 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3149 talloc_free(tmp_ctx);
3153 if (options.machinereadable){
3154 printm(":Name:LinkStatus:References:\n");
3156 printf("Interfaces on node %u\n", options.pnn);
3159 for (i=0; i<ifaces->num; i++) {
3160 if (options.machinereadable){
3161 printm(":%s:%s:%u:\n",
3162 ifaces->ifaces[i].name,
3163 ifaces->ifaces[i].link_state?"1":"0",
3164 (unsigned int)ifaces->ifaces[i].references);
3166 printf("name:%s link:%s references:%u\n",
3167 ifaces->ifaces[i].name,
3168 ifaces->ifaces[i].link_state?"up":"down",
3169 (unsigned int)ifaces->ifaces[i].references);
3173 talloc_free(tmp_ctx);
3179 set link status of an interface
3181 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3184 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3185 struct ctdb_control_iface_info info;
3193 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3194 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3196 talloc_free(tmp_ctx);
3199 strcpy(info.name, argv[0]);
3201 if (strcmp(argv[1], "up") == 0) {
3202 info.link_state = 1;
3203 } else if (strcmp(argv[1], "down") == 0) {
3204 info.link_state = 0;
3206 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3208 talloc_free(tmp_ctx);
3212 /* read the public ip list from this node */
3213 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3216 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3217 argv[0], options.pnn));
3218 talloc_free(tmp_ctx);
3222 talloc_free(tmp_ctx);
3227 display pid of a ctdb daemon
3229 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3234 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3236 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3239 printf("Pid:%d\n", pid);
3244 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3246 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3248 update_flags_handler_t handler,
3253 struct ctdb_node_map_old *nodemap = NULL;
3257 /* Check if the node is already in the desired state */
3258 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3260 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3263 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3264 if (set_flag == flag_is_set) {
3265 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3266 (set_flag ? "already" : "not"), desc));
3271 if (!handler(ctdb, data)) {
3272 DEBUG(DEBUG_WARNING,
3273 ("Failed to send control to set state %s on node %u, try again\n",
3274 desc, options.pnn));
3279 /* Read the nodemap and verify the change took effect.
3280 * Even if the above control/hanlder timed out then it
3281 * could still have worked!
3283 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3286 DEBUG(DEBUG_WARNING,
3287 ("Unable to get nodemap from local node, try again\n"));
3289 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3290 } while (nodemap == NULL || (set_flag != flag_is_set));
3292 return ipreallocate(ctdb);
3295 /* Administratively disable a node */
3296 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3300 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3301 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3305 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3307 return update_flags_and_ipreallocate(ctdb, NULL,
3308 update_flags_disabled,
3309 NODE_FLAGS_PERMANENTLY_DISABLED,
3311 true /* set_flag*/);
3314 /* Administratively re-enable a node */
3315 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3319 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3320 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3324 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3326 return update_flags_and_ipreallocate(ctdb, NULL,
3327 update_flags_not_disabled,
3328 NODE_FLAGS_PERMANENTLY_DISABLED,
3330 false /* set_flag*/);
3334 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3338 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3343 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3345 return update_flags_and_ipreallocate(ctdb, NULL,
3346 update_flags_stopped,
3349 true /* set_flag*/);
3352 /* Continue a stopped node */
3353 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3357 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3362 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3364 return update_flags_and_ipreallocate(ctdb, NULL,
3365 update_flags_not_stopped,
3368 false /* set_flag */);
3371 static uint32_t get_generation(struct ctdb_context *ctdb)
3373 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3374 struct ctdb_vnn_map *vnnmap=NULL;
3376 uint32_t generation;
3378 /* wait until the recmaster is not in recovery mode */
3380 uint32_t recmode, recmaster;
3382 if (vnnmap != NULL) {
3383 talloc_free(vnnmap);
3387 /* get the recmaster */
3388 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3390 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3391 talloc_free(tmp_ctx);
3395 /* get recovery mode */
3396 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3398 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3399 talloc_free(tmp_ctx);
3403 /* get the current generation number */
3404 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3406 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3407 talloc_free(tmp_ctx);
3411 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3412 generation = vnnmap->generation;
3413 talloc_free(tmp_ctx);
3421 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3423 struct ctdb_ban_state *bantime = (struct ctdb_ban_state *)data;
3426 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3431 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3433 struct ctdb_ban_state bantime;
3439 bantime.pnn = options.pnn;
3440 bantime.time = strtoul(argv[0], NULL, 0);
3442 if (bantime.time == 0) {
3443 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3447 return update_flags_and_ipreallocate(ctdb, &bantime,
3448 update_state_banned,
3451 true /* set_flag*/);
3456 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3458 struct ctdb_ban_state bantime;
3460 bantime.pnn = options.pnn;
3463 return update_flags_and_ipreallocate(ctdb, &bantime,
3464 update_state_banned,
3467 false /* set_flag*/);
3471 show ban information for a node
3473 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3476 struct ctdb_node_map_old *nodemap=NULL;
3477 struct ctdb_ban_state *bantime;
3479 /* verify the node exists */
3480 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3482 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3486 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3488 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3492 if (bantime->time == 0) {
3493 printf("Node %u is not banned\n", bantime->pnn);
3495 printf("Node %u is banned, %d seconds remaining\n",
3496 bantime->pnn, bantime->time);
3505 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3509 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3511 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3521 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3524 uint32_t generation, next_generation;
3526 /* record the current generation number */
3527 generation = get_generation(ctdb);
3529 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3531 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3535 /* wait until we are in a new generation */
3537 next_generation = get_generation(ctdb);
3538 if (next_generation != generation) {
3549 display monitoring mode of a remote node
3551 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3556 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3558 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3561 if (!options.machinereadable){
3562 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3565 printm(":%d:\n",monmode);
3572 display capabilities of a remote node
3574 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3576 uint32_t capabilities;
3579 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3581 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3585 if (!options.machinereadable){
3586 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3587 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3588 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3589 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3591 printm(":RECMASTER:LMASTER:LVS:NATGW:\n");
3592 printm(":%d:%d:%d:%d:\n",
3593 !!(capabilities&CTDB_CAP_RECMASTER),
3594 !!(capabilities&CTDB_CAP_LMASTER),
3595 !!(capabilities&CTDB_CAP_LVS),
3596 !!(capabilities&CTDB_CAP_NATGW));
3602 display lvs configuration
3605 static uint32_t lvs_exclude_flags[] = {
3606 /* Look for a nice healthy node */
3607 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3608 /* If not found, an UNHEALTHY node will do */
3609 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3613 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3615 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3616 struct ctdb_node_map_old *orig_nodemap=NULL;
3617 struct ctdb_node_map_old *nodemap;
3620 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3621 tmp_ctx, &orig_nodemap);
3623 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3624 talloc_free(tmp_ctx);
3628 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3629 CTDB_CAP_LVS, false);
3630 if (nodemap == NULL) {
3638 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3639 struct ctdb_node_map_old *t =
3640 filter_nodemap_by_flags(ctdb, nodemap,
3641 lvs_exclude_flags[i]);
3648 /* At least 1 node without excluded flags */
3650 for (j = 0; j < t->num; j++) {
3651 printf("%d:%s\n", t->nodes[j].pnn,
3652 ctdb_addr_to_str(&t->nodes[j].addr));
3659 talloc_free(tmp_ctx);
3664 display who is the lvs master
3666 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3668 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3669 struct ctdb_node_map_old *nodemap=NULL;
3672 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3675 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3676 talloc_free(tmp_ctx);
3680 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3681 struct ctdb_node_map_old *t =
3682 filter_nodemap_by_flags(ctdb, nodemap,
3683 lvs_exclude_flags[i]);
3690 struct ctdb_node_map_old *n;
3691 n = filter_nodemap_by_capabilities(ctdb,
3702 if (options.machinereadable) {
3703 printm("%d\n", n->nodes[0].pnn);
3705 printf("Node %d is LVS master\n", n->nodes[0].pnn);
3713 printf("There is no LVS master\n");
3716 talloc_free(tmp_ctx);
3721 disable monitoring on a node
3723 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3728 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3730 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3733 printf("Monitoring mode:%s\n","DISABLED");
3739 enable monitoring on a node
3741 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3746 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3748 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3751 printf("Monitoring mode:%s\n","ACTIVE");
3757 display remote list of keys/data for a db
3759 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3761 const char *db_name;
3762 struct ctdb_db_context *ctdb_db;
3764 struct ctdb_dump_db_context c;
3771 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3775 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3776 if (ctdb_db == NULL) {
3777 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3781 if (options.printlmaster) {
3782 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3783 ctdb, &ctdb->vnn_map);
3785 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3794 c.printemptyrecords = (bool)options.printemptyrecords;
3795 c.printdatasize = (bool)options.printdatasize;
3796 c.printlmaster = (bool)options.printlmaster;
3797 c.printhash = (bool)options.printhash;
3798 c.printrecordflags = (bool)options.printrecordflags;
3800 /* traverse and dump the cluster tdb */
3801 ret = ctdb_dump_db(ctdb_db, &c);
3803 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3804 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3805 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3809 talloc_free(ctdb_db);
3811 printf("Dumped %d records\n", ret);
3815 struct cattdb_data {
3816 struct ctdb_context *ctdb;
3820 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3822 struct cattdb_data *d = private_data;
3823 struct ctdb_dump_db_context c;
3830 c.printemptyrecords = (bool)options.printemptyrecords;
3831 c.printdatasize = (bool)options.printdatasize;
3832 c.printlmaster = false;
3833 c.printhash = (bool)options.printhash;
3834 c.printrecordflags = true;
3836 return ctdb_dumpdb_record(key, data, &c);
3840 cat the local tdb database using same format as catdb
3842 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3844 const char *db_name;
3845 struct ctdb_db_context *ctdb_db;
3846 struct cattdb_data d;
3853 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3857 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3858 if (ctdb_db == NULL) {
3859 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3863 /* traverse the local tdb */
3866 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3867 printf("Failed to cattdb data\n");
3870 talloc_free(ctdb_db);
3872 printf("Dumped %d records\n", d.count);
3877 display the content of a database key
3879 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3881 const char *db_name;
3882 struct ctdb_db_context *ctdb_db;
3883 struct ctdb_record_handle *h;
3884 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3892 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3896 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3897 if (ctdb_db == NULL) {
3898 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3902 key.dptr = discard_const(argv[1]);
3903 key.dsize = strlen((char *)key.dptr);
3905 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3907 printf("Failed to fetch record '%s' on node %d\n",
3908 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3909 talloc_free(tmp_ctx);
3913 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3915 talloc_free(tmp_ctx);
3916 talloc_free(ctdb_db);
3921 display the content of a database key
3923 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3925 const char *db_name;
3926 struct ctdb_db_context *ctdb_db;
3927 struct ctdb_record_handle *h;
3928 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3936 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3940 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3941 if (ctdb_db == NULL) {
3942 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3946 key.dptr = discard_const(argv[1]);
3947 key.dsize = strlen((char *)key.dptr);
3949 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3951 printf("Failed to fetch record '%s' on node %d\n",
3952 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3953 talloc_free(tmp_ctx);
3957 data.dptr = discard_const(argv[2]);
3958 data.dsize = strlen((char *)data.dptr);
3960 if (ctdb_record_store(h, data) != 0) {
3961 printf("Failed to store record\n");
3965 talloc_free(tmp_ctx);
3966 talloc_free(ctdb_db);
3971 fetch a record from a persistent database
3973 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3975 const char *db_name;
3976 struct ctdb_db_context *ctdb_db;
3977 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3978 struct ctdb_transaction_handle *h;
3985 talloc_free(tmp_ctx);
3989 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3990 talloc_free(tmp_ctx);
3994 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3996 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3997 talloc_free(tmp_ctx);
4001 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4002 if (ctdb_db == NULL) {
4003 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4004 talloc_free(tmp_ctx);
4008 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4010 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4011 talloc_free(tmp_ctx);
4015 key.dptr = discard_const(argv[1]);
4016 key.dsize = strlen(argv[1]);
4017 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
4019 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
4020 talloc_free(tmp_ctx);
4024 if (data.dsize == 0 || data.dptr == NULL) {
4025 DEBUG(DEBUG_ERR,("Record is empty\n"));
4026 talloc_free(tmp_ctx);
4031 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4033 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
4034 talloc_free(tmp_ctx);
4037 sys_write(fd, data.dptr, data.dsize);
4040 sys_write(1, data.dptr, data.dsize);
4043 /* abort the transaction */
4047 talloc_free(tmp_ctx);
4052 fetch a record from a tdb-file
4054 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4056 const char *tdb_file;
4059 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4068 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4070 printf("Failed to open TDB file %s\n", tdb_file);
4074 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
4075 if (key.dptr == NULL) {
4076 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4080 data = tdb_fetch(tdb, key);
4081 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4082 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4090 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4092 printf("Failed to open output file %s\n", argv[2]);
4095 if (options.verbose){
4096 sys_write(fd, data.dptr, data.dsize);
4098 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4102 if (options.verbose){
4103 sys_write(1, data.dptr, data.dsize);
4105 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4109 talloc_free(tmp_ctx);
4114 store a record and header to a tdb-file
4116 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4118 const char *tdb_file;
4120 TDB_DATA key, value, data;
4121 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4122 struct ctdb_ltdb_header header;
4130 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4132 printf("Failed to open TDB file %s\n", tdb_file);
4136 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
4137 if (key.dptr == NULL) {
4138 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4142 value = strtodata(tmp_ctx, argv[2], strlen(argv[2]));
4143 if (value.dptr == NULL) {
4144 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4148 ZERO_STRUCT(header);
4150 header.rsn = atoll(argv[3]);
4153 header.dmaster = atoi(argv[4]);
4156 header.flags = atoi(argv[5]);
4159 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4160 data.dptr = talloc_size(tmp_ctx, data.dsize);
4161 if (data.dptr == NULL) {
4162 printf("Failed to allocate header+value\n");
4166 *(struct ctdb_ltdb_header *)data.dptr = header;
4167 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4169 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4170 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4177 talloc_free(tmp_ctx);
4182 write a record to a persistent database
4184 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4186 const char *db_name;
4187 struct ctdb_db_context *ctdb_db;
4188 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4189 struct ctdb_transaction_handle *h;
4195 talloc_free(tmp_ctx);
4199 fd = open(argv[2], O_RDONLY);
4201 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4202 talloc_free(tmp_ctx);
4206 ret = fstat(fd, &st);
4208 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4210 talloc_free(tmp_ctx);
4214 if (!S_ISREG(st.st_mode)) {
4215 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4217 talloc_free(tmp_ctx);
4221 data.dsize = st.st_size;
4222 if (data.dsize == 0) {
4225 data.dptr = talloc_size(tmp_ctx, data.dsize);
4226 if (data.dptr == NULL) {
4227 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4229 talloc_free(tmp_ctx);
4232 ret = sys_read(fd, data.dptr, data.dsize);
4233 if (ret != data.dsize) {
4234 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4236 talloc_free(tmp_ctx);
4245 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4246 if (ctdb_db == NULL) {
4247 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4248 talloc_free(tmp_ctx);
4252 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4254 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4255 talloc_free(tmp_ctx);
4259 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
4260 if (key.dptr == NULL) {
4261 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4265 ret = ctdb_transaction_store(h, key, data);
4267 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4268 talloc_free(tmp_ctx);
4272 ret = ctdb_transaction_commit(h);
4274 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4275 talloc_free(tmp_ctx);
4280 talloc_free(tmp_ctx);
4285 * delete a record from a persistent database
4287 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4289 const char *db_name;
4290 struct ctdb_db_context *ctdb_db;
4291 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4292 struct ctdb_transaction_handle *h;
4299 talloc_free(tmp_ctx);
4303 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4304 talloc_free(tmp_ctx);
4308 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4310 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4311 talloc_free(tmp_ctx);
4315 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4316 if (ctdb_db == NULL) {
4317 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4318 talloc_free(tmp_ctx);
4322 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4324 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4325 talloc_free(tmp_ctx);
4329 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
4330 if (key.dptr == NULL) {
4331 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4335 ret = ctdb_transaction_store(h, key, tdb_null);
4337 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4338 talloc_free(tmp_ctx);
4342 ret = ctdb_transaction_commit(h);
4344 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4345 talloc_free(tmp_ctx);
4349 talloc_free(tmp_ctx);
4353 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4358 const char *ret; /* Next byte after successfully parsed value */
4360 /* Error, unless someone says otherwise */
4362 /* Indicates no value to parse */
4365 /* Skip whitespace */
4366 n = strspn(s, " \t");
4370 /* Quoted ASCII string - no wide characters! */
4372 n = strcspn(t, "\"");
4375 *data = strtodata(mem_ctx, t, n);
4376 CTDB_NOMEM_ABORT(data->dptr);
4380 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4383 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4389 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4390 TDB_DATA *key, TDB_DATA *value)
4392 char line [1024]; /* FIXME: make this more flexible? */
4396 ptr = fgets(line, sizeof(line), file);
4403 t = ptrans_parse_string(mem_ctx, line, key);
4404 if (t == NULL || key->dptr == NULL) {
4405 /* Line Ignored but not EOF */
4410 t = ptrans_parse_string(mem_ctx, t, value);
4412 /* Line Ignored but not EOF */
4413 talloc_free(key->dptr);
4422 * Update a persistent database as per file/stdin
4424 static int control_ptrans(struct ctdb_context *ctdb,
4425 int argc, const char **argv)
4427 const char *db_name;
4428 struct ctdb_db_context *ctdb_db;
4429 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4430 struct ctdb_transaction_handle *h;
4431 TDB_DATA key, value;
4436 talloc_free(tmp_ctx);
4442 file = fopen(argv[1], "r");
4444 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4445 talloc_free(tmp_ctx);
4452 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4453 if (ctdb_db == NULL) {
4454 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4458 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4460 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4464 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4465 if (key.dsize != 0) {
4466 ret = ctdb_transaction_store(h, key, value);
4467 /* Minimise memory use */
4468 talloc_free(key.dptr);
4469 if (value.dptr != NULL) {
4470 talloc_free(value.dptr);
4473 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4474 ctdb_transaction_cancel(h);
4480 ret = ctdb_transaction_commit(h);
4482 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4486 if (file != stdin) {
4489 talloc_free(tmp_ctx);
4493 if (file != stdin) {
4497 talloc_free(tmp_ctx);
4502 check if a service is bound to a port or not
4504 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4509 struct sockaddr_in sin;
4512 printf("Use: ctdb chktcport <port>\n");
4516 port = atoi(argv[0]);
4518 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4520 printf("Failed to open local socket\n");
4524 v = fcntl(s, F_GETFL, 0);
4525 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4526 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4529 bzero(&sin, sizeof(sin));
4530 sin.sin_family = PF_INET;
4531 sin.sin_port = htons(port);
4532 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4535 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4543 /* Reload public IPs on a specified nodes */
4544 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4546 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4552 assert_single_node_only();
4558 /* Determine the nodes where IPs need to be reloaded */
4559 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4560 options.pnn, true, &nodes, &pnn_mode)) {
4566 /* Disable takeover runs on all connected nodes. A reply
4567 * indicating success is needed from each node so all nodes
4568 * will need to be active. This will retry until maxruntime
4569 * is exceeded, hence no error handling.
4571 * A check could be added to not allow reloading of IPs when
4572 * there are disconnected nodes. However, this should
4573 * probably be left up to the administrator.
4575 timeout = LONGTIMEOUT;
4576 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4577 "Disable takeover runs", true);
4579 /* Now tell all the desired nodes to reload their public IPs.
4580 * Keep trying this until it succeeds. This assumes all
4581 * failures are transient, which might not be true...
4583 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4584 nodes, 0, LONGTIMELIMIT(),
4586 NULL, NULL, NULL) != 0) {
4588 ("Unable to reload IPs on some nodes, try again.\n"));
4592 /* It isn't strictly necessary to wait until takeover runs are
4593 * re-enabled but doing so can't hurt.
4596 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4597 "Enable takeover runs", true);
4603 talloc_free(tmp_ctx);
4608 display a list of the databases on a remote ctdb
4610 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4613 struct ctdb_dbid_map_old *dbmap=NULL;
4615 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4617 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4621 if(options.machinereadable){
4622 printm(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4623 for(i=0;i<dbmap->num;i++){
4624 const char *path = NULL;
4625 const char *name = NULL;
4626 const char *health = NULL;
4631 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4632 dbmap->dbs[i].db_id, ctdb, &path);
4633 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4634 dbmap->dbs[i].db_id, ctdb, &name);
4635 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4636 dbmap->dbs[i].db_id, ctdb, &health);
4637 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4638 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4639 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4640 printm(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4641 dbmap->dbs[i].db_id, name, path,
4642 !!(persistent), !!(sticky),
4643 !!(health), !!(readonly));
4648 printf("Number of databases:%d\n", dbmap->num);
4649 for(i=0;i<dbmap->num;i++){
4650 const char *path = NULL;
4651 const char *name = NULL;
4652 const char *health = NULL;
4657 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &path);
4658 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &name);
4659 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &health);
4660 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4661 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4662 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4663 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4664 dbmap->dbs[i].db_id, name, path,
4665 persistent?" PERSISTENT":"",
4666 sticky?" STICKY":"",
4667 readonly?" READONLY":"",
4668 health?" UNHEALTHY":"");
4675 display the status of a database on a remote ctdb
4677 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4679 const char *db_name;
4682 const char *path = NULL;
4683 const char *health = NULL;
4689 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4693 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4694 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4695 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4696 db_id, db_name, path,
4697 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4698 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4699 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4700 (health ? health : "OK"));
4706 check if the local node is recmaster or not
4707 it will return 1 if this node is the recmaster and 0 if it is not
4708 or if the local ctdb daemon could not be contacted
4710 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4712 uint32_t mypnn, recmaster;
4715 assert_single_node_only();
4717 mypnn = getpnn(ctdb);
4719 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4721 printf("Failed to get the recmaster\n");
4725 if (recmaster != mypnn) {
4726 printf("this node is not the recmaster\n");
4730 printf("this node is the recmaster\n");
4737 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4740 struct timeval tv = timeval_current();
4741 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4743 printf("Unable to get ping response from node %u\n", options.pnn);
4746 printf("response from %u time=%.6f sec (%d clients)\n",
4747 options.pnn, timeval_elapsed(&tv), ret);
4754 get a node's runstate
4756 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4759 enum ctdb_runstate runstate;
4761 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4763 printf("Unable to get runstate response from node %u\n",
4768 enum ctdb_runstate t;
4770 for (i=0; i<argc; i++) {
4772 t = runstate_from_string(argv[i]);
4773 if (t == CTDB_RUNSTATE_UNKNOWN) {
4774 printf("Invalid run state (%s)\n", argv[i]);
4778 if (t == runstate) {
4785 printf("CTDB not in required run state (got %s)\n",
4786 runstate_to_string((enum ctdb_runstate)runstate));
4791 printf("%s\n", runstate_to_string(runstate));
4799 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4810 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4812 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4816 printf("%-23s = %u\n", name, value);
4823 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4834 value = strtoul(argv[1], NULL, 0);
4836 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4838 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4842 DEBUG(DEBUG_WARNING,
4843 ("Setting obsolete tunable variable '%s'\n",
4852 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4858 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4860 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4864 for (i=0;i<count;i++) {
4865 control_getvar(ctdb, 1, &list[i]);
4874 display debug level on a node
4876 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4881 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4883 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4886 const char *desc = get_debug_by_level(level);
4888 /* This should never happen */
4891 if (options.machinereadable){
4892 printm(":Name:Level:\n");
4893 printm(":%s:%d:\n", desc, level);
4895 printf("Node %u is at debug level %s (%d)\n",
4896 options.pnn, desc, level);
4903 display reclock file of a node
4905 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4908 const char *reclock;
4910 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4912 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4915 if (options.machinereadable){
4916 if (reclock != NULL) {
4917 printm("%s", reclock);
4920 if (reclock == NULL) {
4921 printf("No reclock file used.\n");
4923 printf("Reclock file:%s\n", reclock);
4931 set the reclock file of a node
4933 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4936 const char *reclock = NULL;
4940 } else if (argc == 1) {
4946 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4948 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4955 set the natgw state on/off
4957 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4960 uint32_t natgwstate = 0;
4966 if (!strcmp(argv[0], "on")) {
4968 } else if (!strcmp(argv[0], "off")) {
4974 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4976 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4984 set the lmaster role on/off
4986 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4989 uint32_t lmasterrole = 0;
4995 if (!strcmp(argv[0], "on")) {
4997 } else if (!strcmp(argv[0], "off")) {
5003 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
5005 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
5013 set the recmaster role on/off
5015 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5018 uint32_t recmasterrole = 0;
5024 if (!strcmp(argv[0], "on")) {
5026 } else if (!strcmp(argv[0], "off")) {
5032 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5034 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5042 set debug level on a node or all nodes
5044 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5050 printf("You must specify the debug level. Valid levels are:\n");
5051 print_debug_levels(stdout);
5055 if (!parse_debug(argv[0], &level)) {
5056 printf("Invalid debug level, must be one of\n");
5057 print_debug_levels(stdout);
5061 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5063 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5072 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5078 priority = strtol(argv[0], NULL, 0);
5082 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5084 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5086 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5093 attach to a database
5095 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5097 const char *db_name;
5098 struct ctdb_db_context *ctdb_db;
5099 bool persistent = false;
5109 if (strcmp(argv[1], "persistent") != 0) {
5115 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5116 if (ctdb_db == NULL) {
5117 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5125 * detach from a database
5127 static int control_detach(struct ctdb_context *ctdb, int argc,
5132 int ret, i, status = 0;
5133 struct ctdb_node_map_old *nodemap = NULL;
5134 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5141 assert_single_node_only();
5143 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5146 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5147 "when recovery is active\n"));
5148 talloc_free(tmp_ctx);
5152 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5155 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5157 talloc_free(tmp_ctx);
5161 for (i=0; i<nodemap->num; i++) {
5164 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5168 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5172 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5173 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5174 "inactive (stopped or banned) node "
5175 "%u\n", nodemap->nodes[i].pnn));
5176 talloc_free(tmp_ctx);
5180 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5181 nodemap->nodes[i].pnn,
5182 "AllowClientDBAttach",
5185 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5186 "AllowClientDBAttach from node %u\n",
5187 nodemap->nodes[i].pnn));
5188 talloc_free(tmp_ctx);
5193 DEBUG(DEBUG_ERR, ("Database access is still active on "
5194 "node %u. Set AllowClientDBAttach=0 "
5196 nodemap->nodes[i].pnn));
5197 talloc_free(tmp_ctx);
5202 talloc_free(tmp_ctx);
5204 for (i=0; i<argc; i++) {
5205 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5209 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5210 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5211 "cannot be detached\n", argv[i]));
5216 ret = ctdb_detach(ctdb, db_id);
5218 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5230 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5232 struct ctdb_db_priority db_prio;
5239 db_prio.db_id = strtoul(argv[0], NULL, 0);
5240 db_prio.priority = strtoul(argv[1], NULL, 0);
5242 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5244 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5254 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5256 uint32_t db_id, priority;
5263 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5267 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5269 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5273 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5279 set the sticky records capability for a database
5281 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5283 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5291 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5295 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5297 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5298 talloc_free(tmp_ctx);
5302 talloc_free(tmp_ctx);
5307 set the readonly capability for a database
5309 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5311 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5319 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5323 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5325 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5326 talloc_free(tmp_ctx);
5330 talloc_free(tmp_ctx);
5337 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5347 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5351 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5353 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5357 printf("Sequence number:%lld\n", (long long)seqnum);
5363 run an eventscript on a node
5365 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5371 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5374 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5378 data.dptr = (unsigned char *)discard_const(argv[0]);
5379 data.dsize = strlen((char *)data.dptr) + 1;
5381 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5383 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5384 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5385 if (ret != 0 || res != 0) {
5386 if (errmsg != NULL) {
5388 ("Failed to run eventscripts - %s\n", errmsg));
5390 DEBUG(DEBUG_ERR, ("Failed to run eventscripts\n"));
5392 talloc_free(tmp_ctx);
5395 talloc_free(tmp_ctx);
5399 #define DB_VERSION 1
5400 #define MAX_DB_NAME 64
5401 struct db_file_header {
5402 unsigned long version;
5404 unsigned long persistent;
5406 const char name[MAX_DB_NAME];
5409 struct backup_data {
5410 struct ctdb_marshall_buffer *records;
5413 bool traverse_error;
5416 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5418 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5419 struct ctdb_rec_data_old *rec;
5421 /* add the record */
5422 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5424 bd->traverse_error = true;
5425 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5428 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5429 if (bd->records == NULL) {
5430 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5431 bd->traverse_error = true;
5434 bd->records->count++;
5435 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5436 bd->len += rec->length;
5444 * backup a database to a file
5446 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5448 const char *db_name;
5450 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5451 struct db_file_header dbhdr;
5452 struct ctdb_db_context *ctdb_db;
5453 struct backup_data *bd;
5456 const char *reason = NULL;
5460 assert_single_node_only();
5463 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5467 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5471 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5472 db_id, tmp_ctx, &reason);
5474 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5476 talloc_free(tmp_ctx);
5480 uint32_t allow_unhealthy = 0;
5482 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5483 "AllowUnhealthyDBRead",
5486 if (allow_unhealthy != 1) {
5487 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5490 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5492 talloc_free(tmp_ctx);
5496 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5498 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5499 "tunnable AllowUnhealthyDBRead = %u\n",
5503 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5504 if (ctdb_db == NULL) {
5505 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5506 talloc_free(tmp_ctx);
5511 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5513 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5514 talloc_free(tmp_ctx);
5519 bd = talloc_zero(tmp_ctx, struct backup_data);
5521 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5522 talloc_free(tmp_ctx);
5526 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5527 if (bd->records == NULL) {
5528 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5529 talloc_free(tmp_ctx);
5533 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5534 bd->records->db_id = ctdb_db->db_id;
5535 /* traverse the database collecting all records */
5536 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5537 bd->traverse_error) {
5538 DEBUG(DEBUG_ERR,("Traverse error\n"));
5539 talloc_free(tmp_ctx);
5543 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5546 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5548 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5549 talloc_free(tmp_ctx);
5554 dbhdr.version = DB_VERSION;
5555 dbhdr.timestamp = time(NULL);
5556 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5557 dbhdr.size = bd->len;
5558 if (strlen(argv[0]) >= MAX_DB_NAME) {
5559 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5562 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5563 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
5565 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5568 ret = sys_write(fh, bd->records, bd->len);
5570 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5579 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5583 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5585 talloc_free(tmp_ctx);
5590 * restore a database from a file
5592 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5595 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5598 struct db_file_header dbhdr;
5599 struct ctdb_db_context *ctdb_db;
5600 struct ctdb_node_map_old *nodemap=NULL;
5601 struct ctdb_vnn_map *vnnmap=NULL;
5603 struct ctdb_control_transdb w;
5605 uint32_t generation;
5610 assert_single_node_only();
5612 if (argc < 1 || argc > 2) {
5613 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5617 fh = open(argv[0], O_RDONLY);
5619 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5620 talloc_free(tmp_ctx);
5624 sys_read(fh, &dbhdr, sizeof(dbhdr));
5625 if (dbhdr.version != DB_VERSION) {
5626 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5628 talloc_free(tmp_ctx);
5632 dbname = discard_const(dbhdr.name);
5634 dbname = discard_const(argv[1]);
5637 outdata.dsize = dbhdr.size;
5638 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5639 if (outdata.dptr == NULL) {
5640 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5642 talloc_free(tmp_ctx);
5645 sys_read(fh, outdata.dptr, outdata.dsize);
5648 tm = localtime(&dbhdr.timestamp);
5649 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5650 printf("Restoring database '%s' from backup @ %s\n",
5654 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5655 if (ctdb_db == NULL) {
5656 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5657 talloc_free(tmp_ctx);
5661 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5663 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5664 talloc_free(tmp_ctx);
5669 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5671 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5672 talloc_free(tmp_ctx);
5676 /* freeze all nodes */
5677 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5678 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5679 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5685 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5686 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5687 talloc_free(tmp_ctx);
5692 generation = vnnmap->generation;
5693 data.dptr = (void *)&generation;
5694 data.dsize = sizeof(generation);
5696 /* start a cluster wide transaction */
5697 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5698 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5700 TIMELIMIT(), false, data,
5703 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5708 w.db_id = ctdb_db->db_id;
5709 w.transaction_id = generation;
5711 data.dptr = (void *)&w;
5712 data.dsize = sizeof(w);
5714 /* wipe all the remote databases. */
5715 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5716 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5718 TIMELIMIT(), false, data,
5721 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5722 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5723 talloc_free(tmp_ctx);
5727 /* push the database */
5728 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5729 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5731 TIMELIMIT(), false, outdata,
5734 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5735 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5736 talloc_free(tmp_ctx);
5740 data.dptr = (void *)&ctdb_db->db_id;
5741 data.dsize = sizeof(ctdb_db->db_id);
5743 /* mark the database as healthy */
5744 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5745 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5747 TIMELIMIT(), false, data,
5750 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5751 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5752 talloc_free(tmp_ctx);
5756 data.dptr = (void *)&generation;
5757 data.dsize = sizeof(generation);
5759 /* commit all the changes */
5760 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5762 TIMELIMIT(), false, data,
5765 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5766 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5767 talloc_free(tmp_ctx);
5772 /* thaw all nodes */
5773 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5774 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5780 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5781 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5782 talloc_free(tmp_ctx);
5787 talloc_free(tmp_ctx);
5792 * dump a database backup from a file
5794 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5796 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5798 struct db_file_header dbhdr;
5802 struct ctdb_rec_data_old *rec = NULL;
5803 struct ctdb_marshall_buffer *m;
5804 struct ctdb_dump_db_context c;
5806 assert_single_node_only();
5809 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5813 fh = open(argv[0], O_RDONLY);
5815 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5816 talloc_free(tmp_ctx);
5820 sys_read(fh, &dbhdr, sizeof(dbhdr));
5821 if (dbhdr.version != DB_VERSION) {
5822 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5824 talloc_free(tmp_ctx);
5828 outdata.dsize = dbhdr.size;
5829 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5830 if (outdata.dptr == NULL) {
5831 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5833 talloc_free(tmp_ctx);
5836 sys_read(fh, outdata.dptr, outdata.dsize);
5838 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5840 tm = localtime(&dbhdr.timestamp);
5841 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5842 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5843 dbhdr.name, m->db_id, tbuf);
5848 c.printemptyrecords = (bool)options.printemptyrecords;
5849 c.printdatasize = (bool)options.printdatasize;
5850 c.printlmaster = false;
5851 c.printhash = (bool)options.printhash;
5852 c.printrecordflags = (bool)options.printrecordflags;
5854 for (i=0; i < m->count; i++) {
5858 /* we do not want the header splitted, so we pass NULL*/
5859 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5862 ctdb_dumpdb_record(key, data, &c);
5865 printf("Dumped %d records\n", i);
5866 talloc_free(tmp_ctx);
5871 * wipe a database from a file
5873 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5876 const char *db_name;
5878 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5880 struct ctdb_db_context *ctdb_db;
5881 struct ctdb_node_map_old *nodemap = NULL;
5882 struct ctdb_vnn_map *vnnmap = NULL;
5884 struct ctdb_control_transdb w;
5886 uint32_t generation;
5889 assert_single_node_only();
5892 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5896 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5900 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5901 if (ctdb_db == NULL) {
5902 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5904 talloc_free(tmp_ctx);
5908 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5911 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5913 talloc_free(tmp_ctx);
5917 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5920 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5922 talloc_free(tmp_ctx);
5926 /* freeze all nodes */
5927 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5928 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5929 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5936 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5937 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5938 CTDB_RECOVERY_ACTIVE);
5939 talloc_free(tmp_ctx);
5944 generation = vnnmap->generation;
5945 data.dptr = (void *)&generation;
5946 data.dsize = sizeof(generation);
5948 /* start a cluster wide transaction */
5949 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5950 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5952 TIMELIMIT(), false, data,
5956 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5957 "transactions.\n"));
5961 w.db_id = ctdb_db->db_id;
5962 w.transaction_id = generation;
5964 data.dptr = (void *)&w;
5965 data.dsize = sizeof(w);
5967 /* wipe all the remote databases. */
5968 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5969 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5971 TIMELIMIT(), false, data,
5974 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5975 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5976 talloc_free(tmp_ctx);
5980 data.dptr = (void *)&ctdb_db->db_id;
5981 data.dsize = sizeof(ctdb_db->db_id);
5983 /* mark the database as healthy */
5984 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5985 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5987 TIMELIMIT(), false, data,
5990 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5991 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5992 talloc_free(tmp_ctx);
5996 data.dptr = (void *)&generation;
5997 data.dsize = sizeof(generation);
5999 /* commit all the changes */
6000 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
6002 TIMELIMIT(), false, data,
6005 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
6006 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6007 talloc_free(tmp_ctx);
6011 /* thaw all nodes */
6012 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6013 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
6019 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
6020 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6021 talloc_free(tmp_ctx);
6025 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
6027 talloc_free(tmp_ctx);
6034 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6040 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6041 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6042 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6043 if (ret != 0 || res != 0) {
6044 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6045 talloc_free(tmp_ctx);
6048 sys_write(1, data.dptr, data.dsize);
6049 talloc_free(tmp_ctx);
6054 handler for memory dumps
6056 static void mem_dump_handler(uint64_t srvid, TDB_DATA data, void *private_data)
6058 sys_write(1, data.dptr, data.dsize);
6063 dump memory usage on the recovery daemon
6065 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6069 struct ctdb_srvid_message rd;
6071 rd.pnn = ctdb_get_pnn(ctdb);
6072 rd.srvid = getpid();
6074 /* register a message port for receiveing the reply so that we
6075 can receive the reply
6077 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6080 data.dptr = (uint8_t *)&rd;
6081 data.dsize = sizeof(rd);
6083 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6085 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6089 /* this loop will terminate when we have received the reply */
6091 tevent_loop_once(ctdb->ev);
6098 send a message to a srvid
6100 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6102 unsigned long srvid;
6110 srvid = strtoul(argv[0], NULL, 0);
6112 data.dptr = (uint8_t *)discard_const(argv[1]);
6113 data.dsize= strlen(argv[1]);
6115 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6117 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6125 handler for msglisten
6127 static void msglisten_handler(uint64_t srvid, TDB_DATA data,
6132 printf("Message received: ");
6133 for (i=0;i<data.dsize;i++) {
6134 printf("%c", data.dptr[i]);
6140 listen for messages on a messageport
6142 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6148 /* register a message port and listen for messages
6150 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6151 printf("Listening for messages on srvid:%d\n", (int)srvid);
6154 tevent_loop_once(ctdb->ev);
6161 list all nodes in the cluster
6162 we parse the nodes file directly
6164 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6166 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6167 struct ctdb_node_map_old *node_map;
6170 assert_single_node_only();
6172 node_map = read_nodes_file(mem_ctx);
6173 if (node_map == NULL) {
6174 talloc_free(mem_ctx);
6178 for (i = 0; i < node_map->num; i++) {
6181 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
6184 addr = ctdb_addr_to_str(&node_map->nodes[i].addr);
6185 if (options.machinereadable){
6186 printm(":%d:%s:\n", node_map->nodes[i].pnn, addr);
6188 printf("%s\n", addr);
6191 talloc_free(mem_ctx);
6196 /**********************************************************************/
6197 /* reload the nodes file on all nodes */
6199 static void get_nodes_files_callback(struct ctdb_context *ctdb,
6200 uint32_t node_pnn, int32_t res,
6201 TDB_DATA outdata, void *callback_data)
6203 struct ctdb_node_map_old **maps =
6204 talloc_get_type(callback_data, struct ctdb_node_map_old *);
6206 if (outdata.dsize < offsetof(struct ctdb_node_map_old, nodes) ||
6207 outdata.dptr == NULL) {
6209 (__location__ " Invalid return data: %u %p\n",
6210 (unsigned)outdata.dsize, outdata.dptr));
6214 if (node_pnn >= talloc_array_length(maps)) {
6216 (__location__ " unexpected PNN %u\n", node_pnn));
6220 maps[node_pnn] = talloc_memdup(maps, outdata.dptr, outdata.dsize);
6223 static void get_nodes_files_fail_callback(struct ctdb_context *ctdb,
6224 uint32_t node_pnn, int32_t res,
6225 TDB_DATA outdata, void *callback_data)
6228 ("ERROR: Failed to get nodes file from node %u\n", node_pnn));
6231 static struct ctdb_node_map_old **
6232 ctdb_get_nodes_files(struct ctdb_context *ctdb,
6233 TALLOC_CTX *mem_ctx,
6234 struct timeval timeout,
6235 struct ctdb_node_map_old *nodemap)
6239 struct ctdb_node_map_old **maps;
6241 maps = talloc_zero_array(mem_ctx, struct ctdb_node_map_old *, nodemap->num);
6242 CTDB_NO_MEMORY_NULL(ctdb, maps);
6244 nodes = list_of_connected_nodes(ctdb, nodemap, mem_ctx, true);
6246 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_NODES_FILE,
6247 nodes, 0, TIMELIMIT(),
6249 get_nodes_files_callback,
6250 get_nodes_files_fail_callback,
6260 static bool node_files_are_identical(struct ctdb_node_map_old *nm1,
6261 struct ctdb_node_map_old *nm2)
6265 if (nm1->num != nm2->num) {
6268 for (i = 0; i < nm1->num; i++) {
6269 if (memcmp(&nm1->nodes[i], &nm2->nodes[i],
6270 sizeof(struct ctdb_node_and_flags)) != 0) {
6278 static bool check_all_node_files_are_identical(struct ctdb_context *ctdb,
6279 TALLOC_CTX *mem_ctx,
6280 struct timeval timeout,
6281 struct ctdb_node_map_old *nodemap,
6282 struct ctdb_node_map_old *file_nodemap)
6284 static struct ctdb_node_map_old **maps;
6288 maps = ctdb_get_nodes_files(ctdb, mem_ctx, timeout, nodemap);
6293 for (i = 0; i < talloc_array_length(maps); i++) {
6294 if (maps[i] == NULL) {
6297 if (!node_files_are_identical(file_nodemap, maps[i])) {
6299 ("ERROR: Node file on node %u differs from current node (%u)\n",
6300 i, ctdb_get_pnn(ctdb)));
6309 reload the nodes file on the local node
6311 static bool sanity_check_nodes_file_changes(TALLOC_CTX *mem_ctx,
6312 struct ctdb_node_map_old *nodemap,
6313 struct ctdb_node_map_old *file_nodemap)
6316 bool should_abort = false;
6317 bool have_changes = false;
6319 for (i=0; i<nodemap->num; i++) {
6320 if (i >= file_nodemap->num) {
6322 ("ERROR: Node %u (%s) missing from nodes file\n",
6323 nodemap->nodes[i].pnn,
6324 ctdb_addr_to_str(&nodemap->nodes[i].addr)));
6325 should_abort = true;
6328 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED &&
6329 file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6330 /* Node remains deleted */
6332 ("Node %u is unchanged (DELETED)\n",
6333 nodemap->nodes[i].pnn));
6334 } else if (!(nodemap->nodes[i].flags & NODE_FLAGS_DELETED) &&
6335 !(file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED)) {
6336 /* Node not newly nor previously deleted */
6337 if (!ctdb_same_ip(&nodemap->nodes[i].addr,
6338 &file_nodemap->nodes[i].addr)) {
6340 ("ERROR: Node %u has changed IP address (was %s, now %s)\n",
6341 nodemap->nodes[i].pnn,
6342 /* ctdb_addr_to_str() returns a static */
6343 talloc_strdup(mem_ctx,
6344 ctdb_addr_to_str(&nodemap->nodes[i].addr)),
6345 ctdb_addr_to_str(&file_nodemap->nodes[i].addr)));
6346 should_abort = true;
6349 ("Node %u is unchanged\n",
6350 nodemap->nodes[i].pnn));
6351 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
6352 DEBUG(DEBUG_WARNING,
6353 ("WARNING: Node %u is disconnected. You MUST fix this node manually!\n",
6354 nodemap->nodes[i].pnn));
6357 } else if (file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6358 /* Node is being deleted */
6360 ("Node %u is DELETED\n",
6361 nodemap->nodes[i].pnn));
6362 have_changes = true;
6363 if (!(nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED)) {
6365 ("ERROR: Node %u is still connected\n",
6366 nodemap->nodes[i].pnn));
6367 should_abort = true;
6369 } else if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6370 /* Node was previously deleted */
6372 ("Node %u is UNDELETED\n", nodemap->nodes[i].pnn));
6373 have_changes = true;
6379 ("ERROR: Nodes will not be reloaded due to previous error\n"));
6380 talloc_free(mem_ctx);
6384 /* Leftover nodes in file are NEW */
6385 for (; i < file_nodemap->num; i++) {
6386 DEBUG(DEBUG_NOTICE, ("Node %u is NEW\n",
6387 file_nodemap->nodes[i].pnn));
6388 have_changes = true;
6391 return have_changes;
6394 static void reload_nodes_fail_callback(struct ctdb_context *ctdb,
6395 uint32_t node_pnn, int32_t res,
6396 TDB_DATA outdata, void *callback_data)
6398 DEBUG(DEBUG_WARNING,
6399 ("WARNING: Node %u failed to reload nodes. You MUST fix this node manually!\n",
6403 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6406 struct ctdb_node_map_old *nodemap=NULL;
6407 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
6408 struct ctdb_node_map_old *file_nodemap;
6412 assert_current_node_only(ctdb);
6414 /* Load both the current nodemap and the contents of the local
6415 * nodes file. Compare and sanity check them before doing
6418 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6420 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6424 file_nodemap = read_nodes_file(tmp_ctx);
6425 if (file_nodemap == NULL) {
6426 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6427 talloc_free(tmp_ctx);
6431 if (!check_all_node_files_are_identical(ctdb, tmp_ctx, TIMELIMIT(),
6432 nodemap, file_nodemap)) {
6436 if (!sanity_check_nodes_file_changes(tmp_ctx, nodemap, file_nodemap)) {
6438 ("No change in nodes file, skipping unnecessary reload\n"));
6439 talloc_free(tmp_ctx);
6443 /* Now make the changes */
6444 conn = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
6445 for (i = 0; i < talloc_array_length(conn); i++) {
6446 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n",
6450 /* Another timeout could be used, such as ReRecoveryTimeout or
6451 * a new one for this purpose. However, this is the simplest
6453 timeout = options.timelimit;
6454 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
6455 "Disable recoveries", true);
6458 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_NODES_FILE,
6459 conn, 0, TIMELIMIT(),
6461 NULL, reload_nodes_fail_callback,
6465 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
6466 "Enable recoveries", true);
6468 talloc_free(tmp_ctx);
6474 static const struct {
6476 int (*fn)(struct ctdb_context *, int, const char **);
6478 bool without_daemon; /* can be run without daemon running ? */
6481 } ctdb_commands[] = {
6482 { "version", control_version, true, true, "show version of ctdb" },
6483 { "status", control_status, true, false, "show node status" },
6484 { "uptime", control_uptime, true, false, "show node uptime" },
6485 { "ping", control_ping, true, false, "ping all nodes" },
6486 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6487 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6488 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6489 { "listvars", control_listvars, true, false, "list tunable variables"},
6490 { "statistics", control_statistics, false, false, "show statistics" },
6491 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6492 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6493 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6494 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6495 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6496 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6497 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6498 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6499 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6500 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6501 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6502 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6503 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6504 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6505 { "lvs", control_lvs, true, false, "show lvs configuration" },
6506 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6507 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6508 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6509 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6510 { "getdebug", control_getdebug, true, false, "get debug level" },
6511 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6512 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6513 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6514 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6515 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6516 { "disable", control_disable, true, false, "disable a nodes public IP" },
6517 { "enable", control_enable, true, false, "enable a nodes public IP" },
6518 { "stop", control_stop, true, false, "stop a node" },
6519 { "continue", control_continue, true, false, "re-start a stopped node" },
6520 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6521 { "unban", control_unban, true, false, "unban a node" },
6522 { "showban", control_showban, true, false, "show ban information"},
6523 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6524 { "recover", control_recover, true, false, "force recovery" },
6525 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6526 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6527 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6528 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6529 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6530 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6531 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6532 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6533 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6535 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6537 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6538 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6539 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6540 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6541 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6542 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6543 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6544 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6545 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6546 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6547 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6548 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6549 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6550 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6551 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6552 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6553 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6554 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6555 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6556 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6557 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6558 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6559 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6560 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6561 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6562 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6563 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6564 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6565 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6566 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6567 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6568 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6569 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6570 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6571 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6572 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6573 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6574 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6575 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6576 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6577 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6578 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6579 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6580 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6581 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6582 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6583 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6584 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6590 static void usage(void)
6594 "Usage: ctdb [options] <control>\n" \
6596 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6597 " -Y generate machine readable output\n"
6598 " -x <char> specify delimiter for machine readable output\n"
6599 " -v generate verbose output\n"
6600 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6601 printf("Controls:\n");
6602 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6603 printf(" %-15s %-27s %s\n",
6604 ctdb_commands[i].name,
6605 ctdb_commands[i].args?ctdb_commands[i].args:"",
6606 ctdb_commands[i].msg);
6612 static void ctdb_alarm(int sig)
6614 printf("Maximum runtime exceeded - exiting\n");
6621 int main(int argc, const char *argv[])
6623 struct ctdb_context *ctdb;
6624 char *nodestring = NULL;
6625 int machineparsable = 0;
6626 struct poptOption popt_options[] = {
6629 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6630 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6631 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machine readable output", NULL },
6632 { NULL, 'x', POPT_ARG_STRING, &options.machineseparator, 0, "specify separator for machine readable output", "char" },
6633 { NULL, 'X', POPT_ARG_NONE, &machineparsable, 0, "enable machine parsable output with separator |", NULL },
6634 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6635 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6636 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6637 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6638 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6639 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6640 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6644 const char **extra_argv;
6648 struct tevent_context *ev;
6649 const char *control;
6653 /* set some defaults */
6654 options.maxruntime = 0;
6655 options.timelimit = 10;
6656 options.pnn = CTDB_CURRENT_NODE;
6658 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6660 while ((opt = poptGetNextOpt(pc)) != -1) {
6663 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6664 poptBadOption(pc, 0), poptStrerror(opt)));
6669 /* setup the remaining options for the main program to use */
6670 extra_argv = poptGetArgs(pc);
6673 while (extra_argv[extra_argc]) extra_argc++;
6676 if (extra_argc < 1) {
6680 if (options.maxruntime == 0) {
6681 const char *ctdb_timeout;
6682 ctdb_timeout = getenv("CTDB_TIMEOUT");
6683 if (ctdb_timeout != NULL) {
6684 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6686 /* default timeout is 120 seconds */
6687 options.maxruntime = 120;
6691 if (machineparsable) {
6692 options.machineseparator = "|";
6694 if (options.machineseparator != NULL) {
6695 if (strlen(options.machineseparator) != 1) {
6696 printf("Invalid separator \"%s\" - "
6697 "must be single character\n",
6698 options.machineseparator);
6703 options.machinereadable = true;
6704 } else if (options.machinereadable) {
6705 options.machineseparator = ":";
6708 signal(SIGALRM, ctdb_alarm);
6709 alarm(options.maxruntime);
6711 control = extra_argv[0];
6713 /* Default value for CTDB_BASE - don't override */
6714 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6716 ev = tevent_context_init(NULL);
6718 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6722 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6723 if (strcmp(control, ctdb_commands[i].name) == 0) {
6728 if (i == ARRAY_SIZE(ctdb_commands)) {
6729 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6733 if (ctdb_commands[i].without_daemon == true) {
6734 if (nodestring != NULL) {
6735 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6738 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6741 /* initialise ctdb */
6742 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6746 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6748 pnn = find_node_xpnn();
6751 ("Is this node part of a CTDB cluster?\n"));
6756 /* setup the node number(s) to contact */
6757 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6758 &options.nodes, &options.pnn)) {
6762 if (options.pnn == CTDB_CURRENT_NODE) {
6763 options.pnn = options.nodes[0];
6766 if (ctdb_commands[i].auto_all &&
6767 ((options.pnn == CTDB_BROADCAST_ALL) ||
6768 (options.pnn == CTDB_MULTICAST))) {
6772 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6773 options.pnn = options.nodes[j];
6774 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6777 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6782 (void)poptFreeContext(pc);