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"
49 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
50 #define ERR_NONODE 21 /* node does not exist */
51 #define ERR_DISNODE 22 /* node is disconnected */
53 static void usage(void);
60 const char *machineseparator;
63 int printemptyrecords;
70 #define LONGTIMEOUT options.timelimit*10
72 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
73 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
75 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
77 return (tv2->tv_sec - tv->tv_sec) +
78 (tv2->tv_usec - tv->tv_usec)*1.0e-6;
81 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
83 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
87 /* Like printf(3) but substitute for separator in format */
88 static int printm(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
89 static int printm(const char *format, ...)
93 size_t len = strlen(format);
94 char new_format[len+1];
96 strcpy(new_format, format);
98 if (options.machineseparator[0] != ':') {
99 all_string_sub(new_format,
100 ":", options.machineseparator, len + 1);
103 va_start(ap, format);
104 ret = vprintf(new_format, ap);
110 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
111 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
112 "Out of memory in " __location__ )); \
116 static uint32_t getpnn(struct ctdb_context *ctdb)
118 if ((options.pnn == CTDB_BROADCAST_ALL) ||
119 (options.pnn == CTDB_MULTICAST)) {
121 ("Cannot get PNN for node %u\n", options.pnn));
125 if (options.pnn == CTDB_CURRENT_NODE) {
126 return ctdb_get_pnn(ctdb);
132 static void assert_single_node_only(void)
134 if ((options.pnn == CTDB_BROADCAST_ALL) ||
135 (options.pnn == CTDB_MULTICAST)) {
137 ("This control can not be applied to multiple PNNs\n"));
142 static void assert_current_node_only(struct ctdb_context *ctdb)
144 if (options.pnn != ctdb_get_pnn(ctdb)) {
146 ("This control can only be applied to the current node\n"));
151 /* Pretty print the flags to a static buffer in human-readable format.
152 * This never returns NULL!
154 static const char *pretty_print_flags(uint32_t flags)
157 static const struct {
161 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
162 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
163 { NODE_FLAGS_BANNED, "BANNED" },
164 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
165 { NODE_FLAGS_DELETED, "DELETED" },
166 { NODE_FLAGS_STOPPED, "STOPPED" },
167 { NODE_FLAGS_INACTIVE, "INACTIVE" },
169 static char flags_str[512]; /* Big enough to contain all flag names */
172 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
173 if (flags & flag_names[j].flag) {
174 if (flags_str[0] == '\0') {
175 (void) strcpy(flags_str, flag_names[j].name);
177 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
178 (void) strncat(flags_str, flag_names[j].name,
179 sizeof(flags_str)-1);
183 if (flags_str[0] == '\0') {
184 (void) strcpy(flags_str, "OK");
190 static int h2i(char h)
192 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
193 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
197 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
200 TDB_DATA key = {NULL, 0};
203 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
208 key.dptr = talloc_size(mem_ctx, key.dsize);
210 for (i=0; i < len/2; i++) {
211 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
216 static TDB_DATA strtodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
220 if (!strncmp(str, "0x", 2)) {
221 key = hextodata(mem_ctx, str + 2, len - 2);
223 key.dptr = talloc_memdup(mem_ctx, str, len);
230 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
231 * that are disconnected or deleted. If dd_ok is true those nodes are
232 * included in the output list of nodes. If dd_ok is false, those
233 * nodes are filtered from the "all" case and cause an error if
234 * explicitly specified.
236 static bool parse_nodestring(struct ctdb_context *ctdb,
238 const char * nodestring,
239 uint32_t current_pnn,
244 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
247 struct ctdb_node_map *nodemap;
252 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
254 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
255 talloc_free(tmp_ctx);
259 if (nodestring != NULL) {
260 *nodes = talloc_array(mem_ctx, uint32_t, 0);
261 if (*nodes == NULL) {
267 if (strcmp(nodestring, "all") == 0) {
268 *pnn_mode = CTDB_BROADCAST_ALL;
271 for (i = 0; i < nodemap->num; i++) {
272 if ((nodemap->nodes[i].flags &
273 (NODE_FLAGS_DISCONNECTED |
274 NODE_FLAGS_DELETED)) && !dd_ok) {
277 *nodes = talloc_realloc(mem_ctx, *nodes,
279 if (*nodes == NULL) {
289 ns = talloc_strdup(tmp_ctx, nodestring);
290 tok = strtok(ns, ",");
291 while (tok != NULL) {
294 i = (uint32_t)strtoul(tok, &endptr, 0);
295 if (i == 0 && tok == endptr) {
297 ("Invalid node %s\n", tok));
298 talloc_free(tmp_ctx);
301 if (i >= nodemap->num) {
302 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
303 talloc_free(tmp_ctx);
306 if ((nodemap->nodes[i].flags &
307 (NODE_FLAGS_DISCONNECTED |
308 NODE_FLAGS_DELETED)) && !dd_ok) {
309 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
310 talloc_free(tmp_ctx);
313 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
314 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
315 talloc_free(tmp_ctx);
319 *nodes = talloc_realloc(mem_ctx, *nodes,
321 if (*nodes == NULL) {
328 tok = strtok(NULL, ",");
333 *pnn_mode = (*nodes)[0];
335 *pnn_mode = CTDB_MULTICAST;
339 /* default - no nodes specified */
340 *nodes = talloc_array(mem_ctx, uint32_t, 1);
341 if (*nodes == NULL) {
344 *pnn_mode = CTDB_CURRENT_NODE;
346 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
351 talloc_free(tmp_ctx);
355 talloc_free(tmp_ctx);
360 check if a database exists
362 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
363 uint32_t *dbid, const char **dbname, uint8_t *flags)
366 struct ctdb_dbid_map *dbmap=NULL;
367 bool dbid_given = false, found = false;
369 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
370 const char *name = NULL;
372 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
374 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
378 if (strncmp(dbarg, "0x", 2) == 0) {
379 id = strtoul(dbarg, NULL, 0);
383 for(i=0; i<dbmap->num; i++) {
385 if (id == dbmap->dbs[i].dbid) {
390 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
392 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
396 if (strcmp(name, dbarg) == 0) {
397 id = dbmap->dbs[i].dbid;
404 if (found && dbid_given && dbname != NULL) {
405 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
407 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
414 if (dbid) *dbid = id;
415 if (dbname) *dbname = talloc_strdup(ctdb, name);
416 if (flags) *flags = dbmap->dbs[i].flags;
418 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
422 talloc_free(tmp_ctx);
427 see if a process exists
429 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
437 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
438 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
442 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
444 printf("%u:%u exists\n", pnn, pid);
446 printf("%u:%u does not exist\n", pnn, pid);
452 display statistics structure
454 static void show_statistics(struct ctdb_statistics *s, int show_header)
456 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
458 const char *prefix=NULL;
460 int tmp, days, hours, minutes, seconds;
465 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
466 STATISTICS_FIELD(num_clients),
467 STATISTICS_FIELD(frozen),
468 STATISTICS_FIELD(recovering),
469 STATISTICS_FIELD(num_recoveries),
470 STATISTICS_FIELD(client_packets_sent),
471 STATISTICS_FIELD(client_packets_recv),
472 STATISTICS_FIELD(node_packets_sent),
473 STATISTICS_FIELD(node_packets_recv),
474 STATISTICS_FIELD(keepalive_packets_sent),
475 STATISTICS_FIELD(keepalive_packets_recv),
476 STATISTICS_FIELD(node.req_call),
477 STATISTICS_FIELD(node.reply_call),
478 STATISTICS_FIELD(node.req_dmaster),
479 STATISTICS_FIELD(node.reply_dmaster),
480 STATISTICS_FIELD(node.reply_error),
481 STATISTICS_FIELD(node.req_message),
482 STATISTICS_FIELD(node.req_control),
483 STATISTICS_FIELD(node.reply_control),
484 STATISTICS_FIELD(client.req_call),
485 STATISTICS_FIELD(client.req_message),
486 STATISTICS_FIELD(client.req_control),
487 STATISTICS_FIELD(timeouts.call),
488 STATISTICS_FIELD(timeouts.control),
489 STATISTICS_FIELD(timeouts.traverse),
490 STATISTICS_FIELD(locks.num_calls),
491 STATISTICS_FIELD(locks.num_current),
492 STATISTICS_FIELD(locks.num_pending),
493 STATISTICS_FIELD(locks.num_failed),
494 STATISTICS_FIELD(total_calls),
495 STATISTICS_FIELD(pending_calls),
496 STATISTICS_FIELD(childwrite_calls),
497 STATISTICS_FIELD(pending_childwrite_calls),
498 STATISTICS_FIELD(memory_used),
499 STATISTICS_FIELD(max_hop_count),
500 STATISTICS_FIELD(total_ro_delegations),
501 STATISTICS_FIELD(total_ro_revokes),
504 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
513 if (options.machinereadable){
515 printm("CTDB version:");
516 printm("Current time of statistics:");
517 printm("Statistics collected since:");
518 for (i=0;i<ARRAY_SIZE(fields);i++) {
519 printm("%s:", fields[i].name);
521 printm("num_reclock_ctdbd_latency:");
522 printm("min_reclock_ctdbd_latency:");
523 printm("avg_reclock_ctdbd_latency:");
524 printm("max_reclock_ctdbd_latency:");
526 printm("num_reclock_recd_latency:");
527 printm("min_reclock_recd_latency:");
528 printm("avg_reclock_recd_latency:");
529 printm("max_reclock_recd_latency:");
531 printm("num_call_latency:");
532 printm("min_call_latency:");
533 printm("avg_call_latency:");
534 printm("max_call_latency:");
536 printm("num_lockwait_latency:");
537 printm("min_lockwait_latency:");
538 printm("avg_lockwait_latency:");
539 printm("max_lockwait_latency:");
541 printm("num_childwrite_latency:");
542 printm("min_childwrite_latency:");
543 printm("avg_childwrite_latency:");
544 printm("max_childwrite_latency:");
547 printm("%d:", CTDB_PROTOCOL);
548 printm("%d:", (int)s->statistics_current_time.tv_sec);
549 printm("%d:", (int)s->statistics_start_time.tv_sec);
550 for (i=0;i<ARRAY_SIZE(fields);i++) {
551 printm("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
553 printm("%d:", s->reclock.ctdbd.num);
554 printm("%.6f:", s->reclock.ctdbd.min);
555 printm("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
556 printm("%.6f:", s->reclock.ctdbd.max);
558 printm("%d:", s->reclock.recd.num);
559 printm("%.6f:", s->reclock.recd.min);
560 printm("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
561 printm("%.6f:", s->reclock.recd.max);
563 printm("%d:", s->call_latency.num);
564 printm("%.6f:", s->call_latency.min);
565 printm("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
566 printm("%.6f:", s->call_latency.max);
568 printm("%d:", s->childwrite_latency.num);
569 printm("%.6f:", s->childwrite_latency.min);
570 printm("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
571 printm("%.6f:", s->childwrite_latency.max);
574 printf("CTDB version %u\n", CTDB_PROTOCOL);
575 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
576 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
578 for (i=0;i<ARRAY_SIZE(fields);i++) {
579 if (strchr(fields[i].name, '.')) {
580 preflen = strcspn(fields[i].name, ".")+1;
581 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
582 prefix = fields[i].name;
583 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
588 printf(" %*s%-22s%*s%10u\n",
590 fields[i].name+preflen,
592 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
594 printf(" hop_count_buckets:");
595 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
596 printf(" %d", s->hop_count_bucket[i]);
599 printf(" lock_buckets:");
600 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
601 printf(" %d", s->locks.buckets[i]);
604 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);
606 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);
608 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);
610 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);
611 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);
614 talloc_free(tmp_ctx);
618 display remote ctdb statistics combined from all nodes
620 static int control_statistics_all(struct ctdb_context *ctdb)
623 struct ctdb_statistics statistics;
627 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
628 CTDB_NO_MEMORY(ctdb, nodes);
630 ZERO_STRUCT(statistics);
632 for (i=0;i<num_nodes;i++) {
633 struct ctdb_statistics s1;
635 uint32_t *v1 = (uint32_t *)&s1;
636 uint32_t *v2 = (uint32_t *)&statistics;
638 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
639 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
641 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
644 for (j=0;j<num_ints;j++) {
647 statistics.max_hop_count =
648 MAX(statistics.max_hop_count, s1.max_hop_count);
649 statistics.call_latency.max =
650 MAX(statistics.call_latency.max, s1.call_latency.max);
653 printf("Gathered statistics for %u nodes\n", num_nodes);
654 show_statistics(&statistics, 1);
659 display remote ctdb statistics
661 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
664 struct ctdb_statistics statistics;
666 if (options.pnn == CTDB_BROADCAST_ALL) {
667 return control_statistics_all(ctdb);
670 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
672 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
675 show_statistics(&statistics, 1);
681 reset remote ctdb statistics
683 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
687 ret = ctdb_statistics_reset(ctdb, options.pnn);
689 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
697 display remote ctdb rolling statistics
699 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
702 struct ctdb_statistics_wire *stats;
703 int i, num_records = -1;
705 assert_single_node_only();
708 num_records = atoi(argv[0]) - 1;
711 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
713 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
716 for (i=0;i<stats->num;i++) {
717 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
720 show_statistics(&stats->stats[i], i==0);
721 if (i == num_records) {
730 display remote ctdb db statistics
732 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
734 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
735 struct ctdb_db_statistics *dbstat;
745 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
749 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
751 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
752 talloc_free(tmp_ctx);
756 printf("DB Statistics: %s\n", argv[0]);
757 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
758 dbstat->db_ro_delegations);
759 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
760 dbstat->db_ro_delegations);
761 printf(" %s\n", "locks");
762 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
763 dbstat->locks.num_calls);
764 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
765 dbstat->locks.num_failed);
766 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
767 dbstat->locks.num_current);
768 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
769 dbstat->locks.num_pending);
770 printf(" %s", "hop_count_buckets:");
771 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
772 printf(" %d", dbstat->hop_count_bucket[i]);
775 printf(" %s", "lock_buckets:");
776 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
777 printf(" %d", dbstat->locks.buckets[i]);
780 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
781 "locks_latency MIN/AVG/MAX",
782 dbstat->locks.latency.min,
783 (dbstat->locks.latency.num ?
784 dbstat->locks.latency.total /dbstat->locks.latency.num :
786 dbstat->locks.latency.max,
787 dbstat->locks.latency.num);
788 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
789 "vacuum_latency MIN/AVG/MAX",
790 dbstat->vacuum.latency.min,
791 (dbstat->vacuum.latency.num ?
792 dbstat->vacuum.latency.total /dbstat->vacuum.latency.num :
794 dbstat->vacuum.latency.max,
795 dbstat->vacuum.latency.num);
797 for (i=0; i<dbstat->num_hot_keys; i++) {
798 if (dbstat->hot_keys[i].count > 0) {
802 dbstat->num_hot_keys = num_hot_keys;
804 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
805 for (i = 0; i < dbstat->num_hot_keys; i++) {
807 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
808 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
809 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
814 talloc_free(tmp_ctx);
819 display uptime of remote node
821 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
824 struct ctdb_uptime *uptime = NULL;
825 int tmp, days, hours, minutes, seconds;
827 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
829 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
833 if (options.machinereadable){
834 printm(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
835 printm(":%u:%u:%u:%lf\n",
836 (unsigned int)uptime->current_time.tv_sec,
837 (unsigned int)uptime->ctdbd_start_time.tv_sec,
838 (unsigned int)uptime->last_recovery_finished.tv_sec,
839 timeval_delta(&uptime->last_recovery_finished,
840 &uptime->last_recovery_started)
845 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
847 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
855 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
857 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
865 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
867 printf("Duration of last recovery/failover: %lf seconds\n",
868 timeval_delta(&uptime->last_recovery_finished,
869 &uptime->last_recovery_started));
875 show the PNN of the current node
877 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
881 mypnn = getpnn(ctdb);
883 printf("PNN:%d\n", mypnn);
888 static struct ctdb_node_map *read_nodes_file(TALLOC_CTX *mem_ctx)
890 const char *nodes_list;
892 /* read the nodes file */
893 nodes_list = getenv("CTDB_NODES");
894 if (nodes_list == NULL) {
895 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
896 getenv("CTDB_BASE"));
897 if (nodes_list == NULL) {
898 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
903 return ctdb_read_nodes_file(mem_ctx, nodes_list);
907 show the PNN of the current node
908 discover the pnn by loading the nodes file and try to bind to all
909 addresses one at a time until the ip address is found.
911 static int find_node_xpnn(void)
913 TALLOC_CTX *mem_ctx = talloc_new(NULL);
914 struct ctdb_node_map *node_map;
917 node_map = read_nodes_file(mem_ctx);
918 if (node_map == NULL) {
919 talloc_free(mem_ctx);
923 for (i = 0; i < node_map->num; i++) {
924 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
927 if (ctdb_sys_have_ip(&node_map->nodes[i].addr)) {
928 pnn = node_map->nodes[i].pnn;
929 talloc_free(mem_ctx);
934 printf("Failed to detect which PNN this node is\n");
935 talloc_free(mem_ctx);
939 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
943 assert_single_node_only();
945 pnn = find_node_xpnn();
950 printf("PNN:%d\n", pnn);
954 /* Helpers for ctdb status
956 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
958 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
962 if (node->flags == 0) {
963 struct ctdb_control_get_ifaces *ifaces;
965 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
966 tmp_ctx, &ifaces) == 0) {
967 for (j=0; j < ifaces->num; j++) {
968 if (ifaces->ifaces[j].link_state != 0) {
976 talloc_free(tmp_ctx);
981 static void control_status_header_machine(void)
983 printm(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
984 ":Inactive:PartiallyOnline:ThisNode:\n");
987 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
988 struct ctdb_node_and_flags *node)
990 printm(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
991 ctdb_addr_to_str(&node->addr),
992 !!(node->flags&NODE_FLAGS_DISCONNECTED),
993 !!(node->flags&NODE_FLAGS_BANNED),
994 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
995 !!(node->flags&NODE_FLAGS_UNHEALTHY),
996 !!(node->flags&NODE_FLAGS_STOPPED),
997 !!(node->flags&NODE_FLAGS_INACTIVE),
998 is_partially_online(ctdb, node) ? 1 : 0,
999 (node->pnn == mypnn)?'Y':'N');
1004 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
1005 struct ctdb_node_and_flags *node)
1007 printf("pnn:%d %-16s %s%s\n", node->pnn,
1008 ctdb_addr_to_str(&node->addr),
1009 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
1010 node->pnn == mypnn?" (THIS NODE)":"");
1016 display remote ctdb status
1018 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
1020 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1022 struct ctdb_vnn_map *vnnmap=NULL;
1023 struct ctdb_node_map *nodemap=NULL;
1024 uint32_t recmode, recmaster, mypnn;
1025 int num_deleted_nodes = 0;
1028 mypnn = getpnn(ctdb);
1030 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1032 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1033 talloc_free(tmp_ctx);
1037 if (options.machinereadable) {
1038 control_status_header_machine();
1039 for (i=0;i<nodemap->num;i++) {
1040 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1043 (void) control_status_1_machine(ctdb, mypnn,
1044 &nodemap->nodes[i]);
1046 talloc_free(tmp_ctx);
1050 for (i=0; i<nodemap->num; i++) {
1051 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1052 num_deleted_nodes++;
1055 if (num_deleted_nodes == 0) {
1056 printf("Number of nodes:%d\n", nodemap->num);
1058 printf("Number of nodes:%d (including %d deleted nodes)\n",
1059 nodemap->num, num_deleted_nodes);
1061 for(i=0;i<nodemap->num;i++){
1062 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1065 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1068 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1070 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1071 talloc_free(tmp_ctx);
1074 if (vnnmap->generation == INVALID_GENERATION) {
1075 printf("Generation:INVALID\n");
1077 printf("Generation:%d\n",vnnmap->generation);
1079 printf("Size:%d\n",vnnmap->size);
1080 for(i=0;i<vnnmap->size;i++){
1081 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1084 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1086 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1087 talloc_free(tmp_ctx);
1090 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1092 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1094 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1095 talloc_free(tmp_ctx);
1098 printf("Recovery master:%d\n",recmaster);
1100 talloc_free(tmp_ctx);
1104 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1106 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1108 struct ctdb_node_map *nodemap=NULL;
1110 uint32_t pnn_mode, mypnn;
1116 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1117 options.pnn, true, &nodes, &pnn_mode)) {
1121 if (options.machinereadable) {
1122 control_status_header_machine();
1123 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1124 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1127 mypnn = getpnn(ctdb);
1129 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1131 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1132 talloc_free(tmp_ctx);
1138 for (i = 0; i < talloc_array_length(nodes); i++) {
1139 if (options.machinereadable) {
1140 ret |= control_status_1_machine(ctdb, mypnn,
1141 &nodemap->nodes[nodes[i]]);
1143 ret |= control_status_1_human(ctdb, mypnn,
1144 &nodemap->nodes[nodes[i]]);
1148 talloc_free(tmp_ctx);
1152 static struct ctdb_node_map *read_natgw_nodes_file(struct ctdb_context *ctdb,
1153 TALLOC_CTX *mem_ctx)
1155 const char *natgw_list;
1156 struct ctdb_node_map *natgw_nodes = NULL;
1158 natgw_list = getenv("CTDB_NATGW_NODES");
1159 if (natgw_list == NULL) {
1160 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1161 getenv("CTDB_BASE"));
1162 if (natgw_list == NULL) {
1163 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1167 /* The PNNs/flags will be junk but they're not used */
1168 natgw_nodes = ctdb_read_nodes_file(mem_ctx, natgw_list);
1169 if (natgw_nodes == NULL) {
1171 ("Failed to load natgw node list '%s'\n", natgw_list));
1177 /* talloc off the existing nodemap... */
1178 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1180 return talloc_zero_size(nodemap,
1181 offsetof(struct ctdb_node_map, nodes) +
1182 nodemap->num * sizeof(struct ctdb_node_and_flags));
1185 static struct ctdb_node_map *
1186 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1187 struct ctdb_node_map *nodemap,
1188 struct ctdb_node_map *natgw_nodes)
1191 struct ctdb_node_map *ret;
1193 ret = talloc_nodemap(nodemap);
1194 CTDB_NO_MEMORY_NULL(ctdb, ret);
1198 for (i = 0; i < nodemap->num; i++) {
1199 for(j = 0; j < natgw_nodes->num ; j++) {
1200 if (nodemap->nodes[j].flags & NODE_FLAGS_DELETED) {
1203 if (ctdb_same_ip(&natgw_nodes->nodes[j].addr,
1204 &nodemap->nodes[i].addr)) {
1206 ret->nodes[ret->num] = nodemap->nodes[i];
1216 static struct ctdb_node_map *
1217 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1218 struct ctdb_node_map *nodemap,
1219 uint32_t required_capabilities,
1223 uint32_t capabilities;
1224 struct ctdb_node_map *ret;
1226 ret = talloc_nodemap(nodemap);
1227 CTDB_NO_MEMORY_NULL(ctdb, ret);
1231 for (i = 0; i < nodemap->num; i++) {
1234 /* Disconnected nodes have no capabilities! */
1235 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1239 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1240 nodemap->nodes[i].pnn,
1243 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1244 nodemap->nodes[i].pnn));
1248 if (!(capabilities & required_capabilities)) {
1252 ret->nodes[ret->num] = nodemap->nodes[i];
1262 static struct ctdb_node_map *
1263 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1264 struct ctdb_node_map *nodemap,
1265 uint32_t flags_mask)
1268 struct ctdb_node_map *ret;
1270 ret = talloc_nodemap(nodemap);
1271 CTDB_NO_MEMORY_NULL(ctdb, ret);
1275 for (i = 0; i < nodemap->num; i++) {
1276 if (nodemap->nodes[i].flags & flags_mask) {
1280 ret->nodes[ret->num] = nodemap->nodes[i];
1288 display the list of nodes belonging to this natgw configuration
1290 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1292 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1294 struct ctdb_node_map *natgw_nodes = NULL;
1295 struct ctdb_node_map *orig_nodemap=NULL;
1296 struct ctdb_node_map *nodemap;
1297 uint32_t mypnn, pnn;
1300 /* When we have some nodes that could be the NATGW, make a
1301 * series of attempts to find the first node that doesn't have
1302 * certain status flags set.
1304 uint32_t exclude_flags[] = {
1305 /* Look for a nice healthy node */
1306 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1307 /* If not found, an UNHEALTHY/BANNED node will do */
1308 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1309 /* If not found, a STOPPED node will do */
1310 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1314 /* read the natgw nodes file into a linked list */
1315 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1316 if (natgw_nodes == NULL) {
1321 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1322 tmp_ctx, &orig_nodemap);
1324 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1325 talloc_free(tmp_ctx);
1329 /* Get a nodemap that includes only the nodes in the NATGW
1331 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1332 if (nodemap == NULL) {
1337 ret = 2; /* matches ENOENT */
1340 /* For each flag mask... */
1341 for (i = 0; exclude_flags[i] != 0; i++) {
1342 /* ... get a nodemap that excludes nodes with with
1343 * masked flags... */
1344 struct ctdb_node_map *t =
1345 filter_nodemap_by_flags(ctdb, nodemap,
1353 /* ... and find the first node with the NATGW
1355 struct ctdb_node_map *n;
1356 n = filter_nodemap_by_capabilities(ctdb, t,
1366 pnn = n->nodes[0].pnn;
1367 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1374 if (options.machinereadable) {
1375 printm(":Node:IP:\n");
1376 printm(":%d:%s:\n", pnn, ip);
1378 printf("%d %s\n", pnn, ip);
1381 /* print the pruned list of nodes belonging to this natgw list */
1382 mypnn = getpnn(ctdb);
1383 if (options.machinereadable) {
1384 control_status_header_machine();
1386 printf("Number of nodes:%d\n", nodemap->num);
1388 for(i=0;i<nodemap->num;i++){
1389 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1392 if (options.machinereadable) {
1393 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1395 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1400 talloc_free(tmp_ctx);
1405 display the status of the scripts for monitoring (or other events)
1407 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1408 enum ctdb_eventscript_call type)
1410 struct ctdb_scripts_wire *script_status;
1413 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1415 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1419 if (script_status == NULL) {
1420 if (!options.machinereadable) {
1421 printf("%s cycle never run\n",
1422 ctdb_eventscript_call_names[type]);
1427 if (!options.machinereadable) {
1429 for (i=0; i<script_status->num_scripts; i++) {
1430 if (script_status->scripts[i].status != -ENOEXEC) {
1434 printf("%d scripts were executed last %s cycle\n",
1436 ctdb_eventscript_call_names[type]);
1438 for (i=0; i<script_status->num_scripts; i++) {
1439 const char *status = NULL;
1441 /* The ETIME status is ignored for certain events.
1442 * In that case the status is 0, but endtime is not set.
1444 if (script_status->scripts[i].status == 0 &&
1445 timeval_is_zero(&script_status->scripts[i].finished)) {
1446 script_status->scripts[i].status = -ETIME;
1449 switch (script_status->scripts[i].status) {
1451 status = "TIMEDOUT";
1454 status = "DISABLED";
1460 if (script_status->scripts[i].status > 0)
1464 if (options.machinereadable) {
1465 printm(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1466 ctdb_eventscript_call_names[type],
1467 script_status->scripts[i].name,
1468 script_status->scripts[i].status,
1470 (long)script_status->scripts[i].start.tv_sec,
1471 (long)script_status->scripts[i].start.tv_usec,
1472 (long)script_status->scripts[i].finished.tv_sec,
1473 (long)script_status->scripts[i].finished.tv_usec,
1474 script_status->scripts[i].output);
1478 printf("%-20s Status:%s ",
1479 script_status->scripts[i].name, status);
1481 /* Some other error, eg from stat. */
1482 printf("%-20s Status:CANNOT RUN (%s)",
1483 script_status->scripts[i].name,
1484 strerror(-script_status->scripts[i].status));
1486 if (script_status->scripts[i].status >= 0) {
1487 printf("Duration:%.3lf ",
1488 timeval_delta(&script_status->scripts[i].finished,
1489 &script_status->scripts[i].start));
1491 if (script_status->scripts[i].status != -ENOEXEC) {
1493 ctime(&script_status->scripts[i].start.tv_sec));
1494 if (script_status->scripts[i].status != 0) {
1495 printf(" OUTPUT:%s\n",
1496 script_status->scripts[i].output);
1506 static int control_scriptstatus(struct ctdb_context *ctdb,
1507 int argc, const char **argv)
1510 enum ctdb_eventscript_call type, min, max;
1514 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1519 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1523 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1524 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1530 if (type == CTDB_EVENT_MAX) {
1531 if (strcmp(arg, "all") == 0) {
1533 max = CTDB_EVENT_MAX;
1535 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1540 if (options.machinereadable) {
1541 printm(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1544 for (type = min; type < max; type++) {
1545 ret = control_one_scriptstatus(ctdb, type);
1555 enable an eventscript
1557 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1565 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1567 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1575 disable an eventscript
1577 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1585 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1587 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1595 display the pnn of the recovery master
1597 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1602 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1604 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1607 printf("%d\n",recmaster);
1613 add a tickle to a public address
1615 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1617 struct ctdb_tcp_connection t;
1621 assert_single_node_only();
1627 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1628 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1631 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1632 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1636 data.dptr = (uint8_t *)&t;
1637 data.dsize = sizeof(t);
1639 /* tell all nodes about this tcp connection */
1640 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1641 0, data, ctdb, NULL, NULL, NULL, NULL);
1643 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1652 delete a tickle from a node
1654 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1656 struct ctdb_tcp_connection t;
1660 assert_single_node_only();
1666 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1667 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1670 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1671 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1675 data.dptr = (uint8_t *)&t;
1676 data.dsize = sizeof(t);
1678 /* tell all nodes about this tcp connection */
1679 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1680 0, data, ctdb, NULL, NULL, NULL, NULL);
1682 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1691 get a list of all tickles for this pnn
1693 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1695 struct ctdb_control_tcp_tickle_list *list;
1696 ctdb_sock_addr addr;
1700 assert_single_node_only();
1707 port = atoi(argv[1]);
1710 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1711 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1715 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1717 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1721 if (options.machinereadable){
1722 printm(":source ip:port:destination ip:port:\n");
1723 for (i=0;i<list->tickles.num;i++) {
1724 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1727 printm(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1728 printm(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1731 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1732 printf("Num tickles:%u\n", list->tickles.num);
1733 for (i=0;i<list->tickles.num;i++) {
1734 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1737 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1738 printf("DST: %s:%u\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1748 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1750 struct ctdb_all_public_ips *ips;
1751 struct ctdb_public_ip ip;
1754 uint32_t disable_time;
1756 struct ctdb_node_map *nodemap=NULL;
1757 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1760 data.dptr = (uint8_t*)&disable_time;
1761 data.dsize = sizeof(disable_time);
1762 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1764 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1770 /* read the public ip list from the node */
1771 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1773 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1774 talloc_free(tmp_ctx);
1778 for (i=0;i<ips->num;i++) {
1779 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1784 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1785 pnn, ctdb_addr_to_str(addr)));
1786 talloc_free(tmp_ctx);
1793 data.dptr = (uint8_t *)&ip;
1794 data.dsize = sizeof(ip);
1796 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1798 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1799 talloc_free(tmp_ctx);
1803 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1804 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1811 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1812 talloc_free(tmp_ctx);
1816 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1818 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1819 talloc_free(tmp_ctx);
1823 /* update the recovery daemon so it now knows to expect the new
1824 node assignment for this ip.
1826 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1828 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1832 talloc_free(tmp_ctx);
1838 * scans all other nodes and returns a pnn for another node that can host this
1842 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1844 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1845 struct ctdb_all_public_ips *ips;
1846 struct ctdb_node_map *nodemap=NULL;
1850 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1852 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1853 talloc_free(tmp_ctx);
1857 for(i=0;i<nodemap->num;i++){
1858 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1861 if (nodemap->nodes[i].pnn == options.pnn) {
1865 /* read the public ip list from this node */
1866 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1868 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1872 for (j=0;j<ips->num;j++) {
1873 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1874 pnn = nodemap->nodes[i].pnn;
1875 talloc_free(tmp_ctx);
1882 talloc_free(tmp_ctx);
1886 /* If pnn is -1 then try to find a node to move IP to... */
1887 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1889 bool pnn_specified = (pnn == -1 ? false : true);
1892 while (retries < 5) {
1893 if (!pnn_specified) {
1894 pnn = find_other_host_for_public_ip(ctdb, addr);
1899 ("Trying to move public IP to node %u\n", pnn));
1902 if (move_ip(ctdb, addr, pnn) == 0) {
1915 move/failover an ip address to a specific node
1917 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1920 ctdb_sock_addr addr;
1922 assert_single_node_only();
1929 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1930 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1935 if (sscanf(argv[1], "%u", &pnn) != 1) {
1936 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1940 if (!try_moveip(ctdb, &addr, pnn)) {
1941 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1948 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1952 data.dptr = (uint8_t *)&pnn;
1953 data.dsize = sizeof(uint32_t);
1954 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1956 ("Failed to send message to force node %u to be a rebalancing target\n",
1966 rebalance a node by setting it to allow failback and triggering a
1969 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1971 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1976 assert_single_node_only();
1982 /* Determine the nodes where IPs need to be reloaded */
1983 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1984 options.pnn, true, &nodes, &pnn_mode)) {
1989 for (i = 0; i < talloc_array_length(nodes); i++) {
1990 if (!rebalance_node(ctdb, nodes[i])) {
1996 talloc_free(tmp_ctx);
2000 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
2002 struct ctdb_public_ip ip;
2005 uint32_t disable_time;
2007 struct ctdb_node_map *nodemap=NULL;
2008 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2011 data.dptr = (uint8_t*)&disable_time;
2012 data.dsize = sizeof(disable_time);
2013 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
2015 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
2022 data.dptr = (uint8_t *)&ip;
2023 data.dsize = sizeof(ip);
2025 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
2027 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2028 talloc_free(tmp_ctx);
2032 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2033 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
2040 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
2041 talloc_free(tmp_ctx);
2045 talloc_free(tmp_ctx);
2050 release an ip form all nodes and have it re-assigned by recd
2052 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2054 ctdb_sock_addr addr;
2056 assert_single_node_only();
2063 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2064 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2068 if (rebalance_ip(ctdb, &addr) != 0) {
2069 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2076 static int getips_store_callback(void *param, void *data)
2078 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2079 struct ctdb_all_public_ips *ips = param;
2083 ips->ips[i].pnn = node_ip->pnn;
2084 ips->ips[i].addr = node_ip->addr;
2088 static int getips_count_callback(void *param, void *data)
2090 uint32_t *count = param;
2097 static uint32_t *ip_key(ctdb_sock_addr *ip)
2099 static uint32_t key[IP_KEYLEN];
2101 bzero(key, sizeof(key));
2103 switch (ip->sa.sa_family) {
2105 key[0] = ip->ip.sin_addr.s_addr;
2108 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2116 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2123 static void *add_ip_callback(void *parm, void *data)
2129 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2131 struct ctdb_all_public_ips *tmp_ips;
2132 struct ctdb_node_map *nodemap=NULL;
2133 trbt_tree_t *ip_tree;
2137 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2139 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2143 ip_tree = trbt_create(tmp_ctx, 0);
2145 for(i=0;i<nodemap->num;i++){
2146 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2149 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2153 /* read the public ip list from this node */
2154 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2156 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2160 for (j=0; j<tmp_ips->num;j++) {
2161 struct ctdb_public_ip *node_ip;
2163 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2164 node_ip->pnn = tmp_ips->ips[j].pnn;
2165 node_ip->addr = tmp_ips->ips[j].addr;
2167 trbt_insertarray32_callback(ip_tree,
2168 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2172 talloc_free(tmp_ips);
2177 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2179 len = offsetof(struct ctdb_all_public_ips, ips) +
2180 count*sizeof(struct ctdb_public_ip);
2181 tmp_ips = talloc_zero_size(tmp_ctx, len);
2182 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2190 static void ctdb_every_second(struct tevent_context *ev,
2191 struct tevent_timer *te,
2192 struct timeval t, void *p)
2194 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2196 tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
2197 ctdb_every_second, ctdb);
2200 struct srvid_reply_handler_data {
2204 const char *srvid_str;
2207 static void srvid_broadcast_reply_handler(uint64_t srvid, TDB_DATA data,
2210 struct srvid_reply_handler_data *d =
2211 (struct srvid_reply_handler_data *)private_data;
2215 if (data.dsize != sizeof(ret)) {
2216 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2220 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2221 ret = *(int32_t *)data.dptr;
2224 ("%s failed with result %d\n", d->srvid_str, ret));
2228 if (!d->wait_for_all) {
2233 /* Wait for all replies */
2235 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2236 if (d->nodes[i] == ret) {
2238 ("%s reply received from node %u\n",
2239 d->srvid_str, ret));
2242 if (d->nodes[i] != -1) {
2243 /* Found a node that hasn't yet replied */
2249 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2250 * or replies from all connected nodes. arg is the data argument to
2251 * pass in the srvid_request structure - pass 0 if this isn't needed.
2253 static int srvid_broadcast(struct ctdb_context *ctdb,
2254 uint64_t srvid, uint32_t *arg,
2255 const char *srvid_str, bool wait_for_all)
2260 uint64_t reply_srvid;
2261 struct srvid_request request;
2262 struct srvid_request_data request_data;
2263 struct srvid_reply_handler_data reply_data;
2266 ZERO_STRUCT(request);
2268 /* Time ticks to enable timeouts to be processed */
2269 tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
2270 ctdb_every_second, ctdb);
2272 pnn = ctdb_get_pnn(ctdb);
2273 reply_srvid = getpid();
2277 request.srvid = reply_srvid;
2279 data.dptr = (uint8_t *)&request;
2280 data.dsize = sizeof(request);
2282 request_data.pnn = pnn;
2283 request_data.srvid = reply_srvid;
2284 request_data.data = *arg;
2286 data.dptr = (uint8_t *)&request_data;
2287 data.dsize = sizeof(request_data);
2290 /* Register message port for reply from recovery master */
2291 ctdb_client_set_message_handler(ctdb, reply_srvid,
2292 srvid_broadcast_reply_handler,
2295 reply_data.wait_for_all = wait_for_all;
2296 reply_data.nodes = NULL;
2297 reply_data.srvid_str = srvid_str;
2300 reply_data.done = false;
2303 struct ctdb_node_map *nodemap;
2305 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2306 CTDB_CURRENT_NODE, ctdb, &nodemap);
2309 ("Unable to get nodemap from current node, try again\n"));
2314 if (reply_data.nodes != NULL) {
2315 talloc_free(reply_data.nodes);
2317 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2320 talloc_free(nodemap);
2323 /* Send to all connected nodes. Only recmaster replies */
2324 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2327 /* This can only happen if the socket is closed and
2328 * there's no way to recover from that, so don't try
2332 ("Failed to send %s request to connected nodes\n",
2337 tv = timeval_current();
2338 /* This loop terminates the reply is received */
2339 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2340 tevent_loop_once(ctdb->ev);
2343 if (!reply_data.done) {
2345 ("Still waiting for confirmation of %s\n", srvid_str));
2350 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2352 talloc_free(reply_data.nodes);
2357 static int ipreallocate(struct ctdb_context *ctdb)
2359 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2360 "IP reallocation", false);
2364 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2366 return ipreallocate(ctdb);
2370 add a public ip address to a node
2372 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2375 int len, retries = 0;
2377 ctdb_sock_addr addr;
2378 struct ctdb_control_ip_iface *pub;
2379 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2380 struct ctdb_all_public_ips *ips;
2384 talloc_free(tmp_ctx);
2388 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2389 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2390 talloc_free(tmp_ctx);
2394 /* read the public ip list from the node */
2395 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2397 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2398 talloc_free(tmp_ctx);
2401 for (i=0;i<ips->num;i++) {
2402 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2403 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2410 /* Dont timeout. This command waits for an ip reallocation
2411 which sometimes can take wuite a while if there has
2412 been a recent recovery
2416 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2417 pub = talloc_size(tmp_ctx, len);
2418 CTDB_NO_MEMORY(ctdb, pub);
2422 pub->len = strlen(argv[1])+1;
2423 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2426 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2428 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2432 } while (retries < 5 && ret != 0);
2434 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2435 talloc_free(tmp_ctx);
2439 if (rebalance_node(ctdb, options.pnn) != 0) {
2440 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2444 talloc_free(tmp_ctx);
2449 add a public ip address to a node
2451 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2453 ctdb_sock_addr addr;
2460 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2461 printf("Badly formed ip : %s\n", argv[0]);
2465 iface = ctdb_sys_find_ifname(&addr);
2466 if (iface == NULL) {
2467 printf("Failed to get interface name for ip: %s", argv[0]);
2471 printf("IP on interface %s\n", iface);
2478 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2480 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2482 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2483 struct ctdb_node_map *nodemap=NULL;
2484 struct ctdb_all_public_ips *ips;
2487 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2489 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2493 /* remove it from the nodes that are not hosting the ip currently */
2494 for(i=0;i<nodemap->num;i++){
2495 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2498 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2500 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2504 for (j=0;j<ips->num;j++) {
2505 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2513 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2517 options.pnn = nodemap->nodes[i].pnn;
2518 control_delip(ctdb, argc, argv);
2522 /* remove it from every node (also the one hosting it) */
2523 for(i=0;i<nodemap->num;i++){
2524 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2527 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2529 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2533 for (j=0;j<ips->num;j++) {
2534 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2542 options.pnn = nodemap->nodes[i].pnn;
2543 control_delip(ctdb, argc, argv);
2546 talloc_free(tmp_ctx);
2551 delete a public ip address from a node
2553 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2556 ctdb_sock_addr addr;
2557 struct ctdb_control_ip_iface pub;
2558 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2559 struct ctdb_all_public_ips *ips;
2562 talloc_free(tmp_ctx);
2566 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2567 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2571 if (options.pnn == CTDB_BROADCAST_ALL) {
2572 return control_delip_all(ctdb, argc, argv, &addr);
2579 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2581 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2582 talloc_free(tmp_ctx);
2586 for (i=0;i<ips->num;i++) {
2587 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2593 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2594 ctdb_addr_to_str(&addr)));
2595 talloc_free(tmp_ctx);
2599 /* This is an optimisation. If this node is hosting the IP
2600 * then try to move it somewhere else without invoking a full
2601 * takeover run. We don't care if this doesn't work!
2603 if (ips->ips[i].pnn == options.pnn) {
2604 (void) try_moveip(ctdb, &addr, -1);
2607 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2609 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2610 talloc_free(tmp_ctx);
2614 talloc_free(tmp_ctx);
2618 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2619 int argc, const char **argv)
2621 struct ctdb_tcp_connection *killtcp;
2622 int max_entries, current, i;
2623 struct timeval timeout;
2624 char line[128], src[128], dst[128];
2627 struct client_async_data *async_data;
2628 struct ctdb_client_control_state *state;
2638 while (!feof(stdin)) {
2639 if (fgets(line, sizeof(line), stdin) == NULL) {
2643 /* Silently skip empty lines */
2644 if (line[0] == '\n') {
2648 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2649 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2651 talloc_free(killtcp);
2655 if (current >= max_entries) {
2656 max_entries += 1024;
2657 killtcp = talloc_realloc(ctdb, killtcp,
2658 struct ctdb_tcp_connection,
2660 CTDB_NO_MEMORY(ctdb, killtcp);
2663 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2664 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2666 talloc_free(killtcp);
2670 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2671 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2673 talloc_free(killtcp);
2680 async_data = talloc_zero(ctdb, struct client_async_data);
2681 if (async_data == NULL) {
2682 talloc_free(killtcp);
2686 for (i = 0; i < current; i++) {
2688 data.dsize = sizeof(struct ctdb_tcp_connection);
2689 data.dptr = (unsigned char *)&killtcp[i];
2691 timeout = TIMELIMIT();
2692 state = ctdb_control_send(ctdb, options.pnn, 0,
2693 CTDB_CONTROL_KILL_TCP, 0, data,
2694 async_data, &timeout, NULL);
2696 if (state == NULL) {
2698 ("Failed to call async killtcp control to node %u\n",
2700 talloc_free(killtcp);
2704 ctdb_client_async_add(async_data, state);
2707 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2708 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2709 talloc_free(killtcp);
2713 talloc_free(killtcp);
2719 kill a tcp connection
2721 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2724 struct ctdb_tcp_connection killtcp;
2726 assert_single_node_only();
2729 return kill_tcp_from_file(ctdb, argc, argv);
2736 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2737 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2741 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2742 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2746 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2748 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2759 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2762 ctdb_sock_addr addr;
2764 assert_single_node_only();
2770 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2771 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2775 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2777 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2785 register a server id
2787 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2790 struct ctdb_server_id server_id;
2796 server_id.pnn = strtoul(argv[0], NULL, 0);
2797 server_id.type = strtoul(argv[1], NULL, 0);
2798 server_id.server_id = strtoul(argv[2], NULL, 0);
2800 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2802 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2805 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2811 unregister a server id
2813 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2816 struct ctdb_server_id server_id;
2822 server_id.pnn = strtoul(argv[0], NULL, 0);
2823 server_id.type = strtoul(argv[1], NULL, 0);
2824 server_id.server_id = strtoul(argv[2], NULL, 0);
2826 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2828 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2835 check if a server id exists
2837 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2839 uint32_t status = 0;
2841 struct ctdb_server_id server_id;
2847 server_id.pnn = strtoul(argv[0], NULL, 0);
2848 server_id.type = strtoul(argv[1], NULL, 0);
2849 server_id.server_id = strtoul(argv[2], NULL, 0);
2851 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2853 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2858 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2860 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2866 get a list of all server ids that are registered on a node
2868 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2871 struct ctdb_server_id_list *server_ids;
2873 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2875 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2879 for (i=0; i<server_ids->num; i++) {
2880 printf("Server id %d:%d:%d\n",
2881 server_ids->server_ids[i].pnn,
2882 server_ids->server_ids[i].type,
2883 server_ids->server_ids[i].server_id);
2890 check if a server id exists
2892 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2894 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2900 talloc_free(tmp_ctx);
2904 ids = talloc_array(tmp_ctx, uint64_t, argc);
2905 result = talloc_array(tmp_ctx, uint8_t, argc);
2907 for (i = 0; i < argc; i++) {
2908 ids[i] = strtoull(argv[i], NULL, 0);
2911 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2912 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2914 talloc_free(tmp_ctx);
2918 for (i=0; i < argc; i++) {
2919 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2920 result[i] ? "exists" : "does not exist");
2923 talloc_free(tmp_ctx);
2928 send a tcp tickle ack
2930 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2933 ctdb_sock_addr src, dst;
2939 if (!parse_ip_port(argv[0], &src)) {
2940 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2944 if (!parse_ip_port(argv[1], &dst)) {
2945 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2949 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2953 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2960 display public ip status
2962 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2965 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2966 struct ctdb_all_public_ips *ips;
2968 if (argc == 1 && strcmp(argv[0], "all") == 0) {
2969 options.pnn = CTDB_BROADCAST_ALL;
2972 if (options.pnn == CTDB_BROADCAST_ALL) {
2973 /* read the list of public ips from all nodes */
2974 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2976 /* read the public ip list from this node */
2977 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2980 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2981 talloc_free(tmp_ctx);
2985 if (options.machinereadable){
2986 printm(":Public IP:Node:");
2987 if (options.verbose){
2988 printm("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2992 if (options.pnn == CTDB_BROADCAST_ALL) {
2993 printf("Public IPs on ALL nodes\n");
2995 printf("Public IPs on node %u\n", options.pnn);
2999 for (i=1;i<=ips->num;i++) {
3000 struct ctdb_control_public_ip_info *info = NULL;
3002 char *aciface = NULL;
3003 char *avifaces = NULL;
3004 char *cifaces = NULL;
3006 if (options.pnn == CTDB_BROADCAST_ALL) {
3007 pnn = ips->ips[ips->num-i].pnn;
3013 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
3014 &ips->ips[ips->num-i].addr, &info);
3021 for (j=0; j < info->num; j++) {
3022 if (cifaces == NULL) {
3023 cifaces = talloc_strdup(info,
3024 info->ifaces[j].name);
3026 cifaces = talloc_asprintf_append(cifaces,
3028 info->ifaces[j].name);
3031 if (info->active_idx == j) {
3032 aciface = info->ifaces[j].name;
3035 if (info->ifaces[j].link_state == 0) {
3039 if (avifaces == NULL) {
3040 avifaces = talloc_strdup(info, info->ifaces[j].name);
3042 avifaces = talloc_asprintf_append(avifaces,
3044 info->ifaces[j].name);
3049 if (options.machinereadable){
3051 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3052 ips->ips[ips->num-i].pnn);
3053 if (options.verbose){
3056 avifaces?avifaces:"",
3057 cifaces?cifaces:"");
3061 if (options.verbose) {
3062 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3063 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3064 ips->ips[ips->num-i].pnn,
3066 avifaces?avifaces:"",
3067 cifaces?cifaces:"");
3070 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3071 ips->ips[ips->num-i].pnn);
3077 talloc_free(tmp_ctx);
3084 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3087 ctdb_sock_addr addr;
3088 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3089 struct ctdb_control_public_ip_info *info;
3092 talloc_free(tmp_ctx);
3096 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3097 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3101 /* read the public ip info from this node */
3102 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3103 tmp_ctx, &addr, &info);
3105 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3106 argv[0], options.pnn));
3107 talloc_free(tmp_ctx);
3111 printf("Public IP[%s] info on node %u\n",
3112 ctdb_addr_to_str(&info->ip.addr),
3115 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3116 ctdb_addr_to_str(&info->ip.addr),
3117 info->ip.pnn, info->num);
3119 for (i=0; i<info->num; i++) {
3120 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3122 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3123 i+1, info->ifaces[i].name,
3124 info->ifaces[i].link_state?"up":"down",
3125 (unsigned int)info->ifaces[i].references,
3126 (i==info->active_idx)?" (active)":"");
3129 talloc_free(tmp_ctx);
3134 display interfaces status
3136 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3138 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3140 struct ctdb_control_get_ifaces *ifaces;
3143 /* read the public ip list from this node */
3144 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3146 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3148 talloc_free(tmp_ctx);
3152 if (options.machinereadable){
3153 printm(":Name:LinkStatus:References:\n");
3155 printf("Interfaces on node %u\n", options.pnn);
3158 for (i=0; i<ifaces->num; i++) {
3159 if (options.machinereadable){
3160 printm(":%s:%s:%u:\n",
3161 ifaces->ifaces[i].name,
3162 ifaces->ifaces[i].link_state?"1":"0",
3163 (unsigned int)ifaces->ifaces[i].references);
3165 printf("name:%s link:%s references:%u\n",
3166 ifaces->ifaces[i].name,
3167 ifaces->ifaces[i].link_state?"up":"down",
3168 (unsigned int)ifaces->ifaces[i].references);
3172 talloc_free(tmp_ctx);
3178 set link status of an interface
3180 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3183 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3184 struct ctdb_control_iface_info info;
3192 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3193 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3195 talloc_free(tmp_ctx);
3198 strcpy(info.name, argv[0]);
3200 if (strcmp(argv[1], "up") == 0) {
3201 info.link_state = 1;
3202 } else if (strcmp(argv[1], "down") == 0) {
3203 info.link_state = 0;
3205 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3207 talloc_free(tmp_ctx);
3211 /* read the public ip list from this node */
3212 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3215 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3216 argv[0], options.pnn));
3217 talloc_free(tmp_ctx);
3221 talloc_free(tmp_ctx);
3226 display pid of a ctdb daemon
3228 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3233 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3235 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3238 printf("Pid:%d\n", pid);
3243 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3245 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3247 update_flags_handler_t handler,
3252 struct ctdb_node_map *nodemap = NULL;
3256 /* Check if the node is already in the desired state */
3257 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3259 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3262 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3263 if (set_flag == flag_is_set) {
3264 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3265 (set_flag ? "already" : "not"), desc));
3270 if (!handler(ctdb, data)) {
3271 DEBUG(DEBUG_WARNING,
3272 ("Failed to send control to set state %s on node %u, try again\n",
3273 desc, options.pnn));
3278 /* Read the nodemap and verify the change took effect.
3279 * Even if the above control/hanlder timed out then it
3280 * could still have worked!
3282 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3285 DEBUG(DEBUG_WARNING,
3286 ("Unable to get nodemap from local node, try again\n"));
3288 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3289 } while (nodemap == NULL || (set_flag != flag_is_set));
3291 return ipreallocate(ctdb);
3294 /* Administratively disable a node */
3295 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3299 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3300 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3304 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3306 return update_flags_and_ipreallocate(ctdb, NULL,
3307 update_flags_disabled,
3308 NODE_FLAGS_PERMANENTLY_DISABLED,
3310 true /* set_flag*/);
3313 /* Administratively re-enable a node */
3314 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3318 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3319 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3323 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3325 return update_flags_and_ipreallocate(ctdb, NULL,
3326 update_flags_not_disabled,
3327 NODE_FLAGS_PERMANENTLY_DISABLED,
3329 false /* set_flag*/);
3333 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3337 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3342 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3344 return update_flags_and_ipreallocate(ctdb, NULL,
3345 update_flags_stopped,
3348 true /* set_flag*/);
3351 /* Continue a stopped node */
3352 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3356 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3361 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3363 return update_flags_and_ipreallocate(ctdb, NULL,
3364 update_flags_not_stopped,
3367 false /* set_flag */);
3370 static uint32_t get_generation(struct ctdb_context *ctdb)
3372 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3373 struct ctdb_vnn_map *vnnmap=NULL;
3375 uint32_t generation;
3377 /* wait until the recmaster is not in recovery mode */
3379 uint32_t recmode, recmaster;
3381 if (vnnmap != NULL) {
3382 talloc_free(vnnmap);
3386 /* get the recmaster */
3387 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3389 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3390 talloc_free(tmp_ctx);
3394 /* get recovery mode */
3395 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3397 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3398 talloc_free(tmp_ctx);
3402 /* get the current generation number */
3403 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3405 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3406 talloc_free(tmp_ctx);
3410 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3411 generation = vnnmap->generation;
3412 talloc_free(tmp_ctx);
3420 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3422 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3425 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3430 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3432 struct ctdb_ban_time bantime;
3438 bantime.pnn = options.pnn;
3439 bantime.time = strtoul(argv[0], NULL, 0);
3441 if (bantime.time == 0) {
3442 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3446 return update_flags_and_ipreallocate(ctdb, &bantime,
3447 update_state_banned,
3450 true /* set_flag*/);
3455 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3457 struct ctdb_ban_time bantime;
3459 bantime.pnn = options.pnn;
3462 return update_flags_and_ipreallocate(ctdb, &bantime,
3463 update_state_banned,
3466 false /* set_flag*/);
3470 show ban information for a node
3472 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3475 struct ctdb_node_map *nodemap=NULL;
3476 struct ctdb_ban_time *bantime;
3478 /* verify the node exists */
3479 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3481 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3485 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3487 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3491 if (bantime->time == 0) {
3492 printf("Node %u is not banned\n", bantime->pnn);
3494 printf("Node %u is banned, %d seconds remaining\n",
3495 bantime->pnn, bantime->time);
3504 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3508 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3510 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3520 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3523 uint32_t generation, next_generation;
3525 /* record the current generation number */
3526 generation = get_generation(ctdb);
3528 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3530 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3534 /* wait until we are in a new generation */
3536 next_generation = get_generation(ctdb);
3537 if (next_generation != generation) {
3548 display monitoring mode of a remote node
3550 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3555 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3557 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3560 if (!options.machinereadable){
3561 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3564 printm(":%d:\n",monmode);
3571 display capabilities of a remote node
3573 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3575 uint32_t capabilities;
3578 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3580 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3584 if (!options.machinereadable){
3585 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3586 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3587 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3588 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3590 printm(":RECMASTER:LMASTER:LVS:NATGW:\n");
3591 printm(":%d:%d:%d:%d:\n",
3592 !!(capabilities&CTDB_CAP_RECMASTER),
3593 !!(capabilities&CTDB_CAP_LMASTER),
3594 !!(capabilities&CTDB_CAP_LVS),
3595 !!(capabilities&CTDB_CAP_NATGW));
3601 display lvs configuration
3604 static uint32_t lvs_exclude_flags[] = {
3605 /* Look for a nice healthy node */
3606 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3607 /* If not found, an UNHEALTHY node will do */
3608 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3612 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3614 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3615 struct ctdb_node_map *orig_nodemap=NULL;
3616 struct ctdb_node_map *nodemap;
3619 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3620 tmp_ctx, &orig_nodemap);
3622 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3623 talloc_free(tmp_ctx);
3627 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3628 CTDB_CAP_LVS, false);
3629 if (nodemap == NULL) {
3637 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3638 struct ctdb_node_map *t =
3639 filter_nodemap_by_flags(ctdb, nodemap,
3640 lvs_exclude_flags[i]);
3647 /* At least 1 node without excluded flags */
3649 for (j = 0; j < t->num; j++) {
3650 printf("%d:%s\n", t->nodes[j].pnn,
3651 ctdb_addr_to_str(&t->nodes[j].addr));
3658 talloc_free(tmp_ctx);
3663 display who is the lvs master
3665 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3667 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3668 struct ctdb_node_map *nodemap=NULL;
3671 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3674 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3675 talloc_free(tmp_ctx);
3679 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3680 struct ctdb_node_map *t =
3681 filter_nodemap_by_flags(ctdb, nodemap,
3682 lvs_exclude_flags[i]);
3689 struct ctdb_node_map *n;
3690 n = filter_nodemap_by_capabilities(ctdb,
3701 if (options.machinereadable) {
3702 printm("%d\n", n->nodes[0].pnn);
3704 printf("Node %d is LVS master\n", n->nodes[0].pnn);
3712 printf("There is no LVS master\n");
3715 talloc_free(tmp_ctx);
3720 disable monitoring on a node
3722 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3727 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3729 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3732 printf("Monitoring mode:%s\n","DISABLED");
3738 enable monitoring on a node
3740 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3745 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3747 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3750 printf("Monitoring mode:%s\n","ACTIVE");
3756 display remote list of keys/data for a db
3758 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3760 const char *db_name;
3761 struct ctdb_db_context *ctdb_db;
3763 struct ctdb_dump_db_context c;
3770 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3774 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3775 if (ctdb_db == NULL) {
3776 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3780 if (options.printlmaster) {
3781 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3782 ctdb, &ctdb->vnn_map);
3784 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3793 c.printemptyrecords = (bool)options.printemptyrecords;
3794 c.printdatasize = (bool)options.printdatasize;
3795 c.printlmaster = (bool)options.printlmaster;
3796 c.printhash = (bool)options.printhash;
3797 c.printrecordflags = (bool)options.printrecordflags;
3799 /* traverse and dump the cluster tdb */
3800 ret = ctdb_dump_db(ctdb_db, &c);
3802 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3803 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3804 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3808 talloc_free(ctdb_db);
3810 printf("Dumped %d records\n", ret);
3814 struct cattdb_data {
3815 struct ctdb_context *ctdb;
3819 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3821 struct cattdb_data *d = private_data;
3822 struct ctdb_dump_db_context c;
3829 c.printemptyrecords = (bool)options.printemptyrecords;
3830 c.printdatasize = (bool)options.printdatasize;
3831 c.printlmaster = false;
3832 c.printhash = (bool)options.printhash;
3833 c.printrecordflags = true;
3835 return ctdb_dumpdb_record(key, data, &c);
3839 cat the local tdb database using same format as catdb
3841 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3843 const char *db_name;
3844 struct ctdb_db_context *ctdb_db;
3845 struct cattdb_data d;
3852 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3856 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3857 if (ctdb_db == NULL) {
3858 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3862 /* traverse the local tdb */
3865 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3866 printf("Failed to cattdb data\n");
3869 talloc_free(ctdb_db);
3871 printf("Dumped %d records\n", d.count);
3876 display the content of a database key
3878 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3880 const char *db_name;
3881 struct ctdb_db_context *ctdb_db;
3882 struct ctdb_record_handle *h;
3883 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3891 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3895 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3896 if (ctdb_db == NULL) {
3897 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3901 key.dptr = discard_const(argv[1]);
3902 key.dsize = strlen((char *)key.dptr);
3904 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3906 printf("Failed to fetch record '%s' on node %d\n",
3907 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3908 talloc_free(tmp_ctx);
3912 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3914 talloc_free(tmp_ctx);
3915 talloc_free(ctdb_db);
3920 display the content of a database key
3922 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3924 const char *db_name;
3925 struct ctdb_db_context *ctdb_db;
3926 struct ctdb_record_handle *h;
3927 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3935 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3939 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3940 if (ctdb_db == NULL) {
3941 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3945 key.dptr = discard_const(argv[1]);
3946 key.dsize = strlen((char *)key.dptr);
3948 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3950 printf("Failed to fetch record '%s' on node %d\n",
3951 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3952 talloc_free(tmp_ctx);
3956 data.dptr = discard_const(argv[2]);
3957 data.dsize = strlen((char *)data.dptr);
3959 if (ctdb_record_store(h, data) != 0) {
3960 printf("Failed to store record\n");
3964 talloc_free(tmp_ctx);
3965 talloc_free(ctdb_db);
3970 fetch a record from a persistent database
3972 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3974 const char *db_name;
3975 struct ctdb_db_context *ctdb_db;
3976 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3977 struct ctdb_transaction_handle *h;
3984 talloc_free(tmp_ctx);
3988 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3989 talloc_free(tmp_ctx);
3993 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3995 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3996 talloc_free(tmp_ctx);
4000 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4001 if (ctdb_db == NULL) {
4002 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4003 talloc_free(tmp_ctx);
4007 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4009 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4010 talloc_free(tmp_ctx);
4014 key.dptr = discard_const(argv[1]);
4015 key.dsize = strlen(argv[1]);
4016 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
4018 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
4019 talloc_free(tmp_ctx);
4023 if (data.dsize == 0 || data.dptr == NULL) {
4024 DEBUG(DEBUG_ERR,("Record is empty\n"));
4025 talloc_free(tmp_ctx);
4030 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4032 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
4033 talloc_free(tmp_ctx);
4036 sys_write(fd, data.dptr, data.dsize);
4039 sys_write(1, data.dptr, data.dsize);
4042 /* abort the transaction */
4046 talloc_free(tmp_ctx);
4051 fetch a record from a tdb-file
4053 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4055 const char *tdb_file;
4058 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4067 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4069 printf("Failed to open TDB file %s\n", tdb_file);
4073 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
4074 if (key.dptr == NULL) {
4075 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4079 data = tdb_fetch(tdb, key);
4080 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4081 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4089 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4091 printf("Failed to open output file %s\n", argv[2]);
4094 if (options.verbose){
4095 sys_write(fd, data.dptr, data.dsize);
4097 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4101 if (options.verbose){
4102 sys_write(1, data.dptr, data.dsize);
4104 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4108 talloc_free(tmp_ctx);
4113 store a record and header to a tdb-file
4115 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4117 const char *tdb_file;
4119 TDB_DATA key, value, data;
4120 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4121 struct ctdb_ltdb_header header;
4129 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4131 printf("Failed to open TDB file %s\n", tdb_file);
4135 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
4136 if (key.dptr == NULL) {
4137 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4141 value = strtodata(tmp_ctx, argv[2], strlen(argv[2]));
4142 if (value.dptr == NULL) {
4143 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4147 ZERO_STRUCT(header);
4149 header.rsn = atoll(argv[3]);
4152 header.dmaster = atoi(argv[4]);
4155 header.flags = atoi(argv[5]);
4158 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4159 data.dptr = talloc_size(tmp_ctx, data.dsize);
4160 if (data.dptr == NULL) {
4161 printf("Failed to allocate header+value\n");
4165 *(struct ctdb_ltdb_header *)data.dptr = header;
4166 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4168 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4169 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4176 talloc_free(tmp_ctx);
4181 write a record to a persistent database
4183 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4185 const char *db_name;
4186 struct ctdb_db_context *ctdb_db;
4187 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4188 struct ctdb_transaction_handle *h;
4194 talloc_free(tmp_ctx);
4198 fd = open(argv[2], O_RDONLY);
4200 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4201 talloc_free(tmp_ctx);
4205 ret = fstat(fd, &st);
4207 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4209 talloc_free(tmp_ctx);
4213 if (!S_ISREG(st.st_mode)) {
4214 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4216 talloc_free(tmp_ctx);
4220 data.dsize = st.st_size;
4221 if (data.dsize == 0) {
4224 data.dptr = talloc_size(tmp_ctx, data.dsize);
4225 if (data.dptr == NULL) {
4226 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4228 talloc_free(tmp_ctx);
4231 ret = sys_read(fd, data.dptr, data.dsize);
4232 if (ret != data.dsize) {
4233 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4235 talloc_free(tmp_ctx);
4244 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4245 if (ctdb_db == NULL) {
4246 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4247 talloc_free(tmp_ctx);
4251 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4253 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4254 talloc_free(tmp_ctx);
4258 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
4259 if (key.dptr == NULL) {
4260 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4264 ret = ctdb_transaction_store(h, key, data);
4266 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4267 talloc_free(tmp_ctx);
4271 ret = ctdb_transaction_commit(h);
4273 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4274 talloc_free(tmp_ctx);
4279 talloc_free(tmp_ctx);
4284 * delete a record from a persistent database
4286 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4288 const char *db_name;
4289 struct ctdb_db_context *ctdb_db;
4290 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4291 struct ctdb_transaction_handle *h;
4298 talloc_free(tmp_ctx);
4302 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4303 talloc_free(tmp_ctx);
4307 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4309 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4310 talloc_free(tmp_ctx);
4314 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4315 if (ctdb_db == NULL) {
4316 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4317 talloc_free(tmp_ctx);
4321 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4323 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4324 talloc_free(tmp_ctx);
4328 key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
4329 if (key.dptr == NULL) {
4330 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4334 ret = ctdb_transaction_store(h, key, tdb_null);
4336 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4337 talloc_free(tmp_ctx);
4341 ret = ctdb_transaction_commit(h);
4343 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4344 talloc_free(tmp_ctx);
4348 talloc_free(tmp_ctx);
4352 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4357 const char *ret; /* Next byte after successfully parsed value */
4359 /* Error, unless someone says otherwise */
4361 /* Indicates no value to parse */
4364 /* Skip whitespace */
4365 n = strspn(s, " \t");
4369 /* Quoted ASCII string - no wide characters! */
4371 n = strcspn(t, "\"");
4374 *data = strtodata(mem_ctx, t, n);
4375 CTDB_NOMEM_ABORT(data->dptr);
4379 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4382 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4388 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4389 TDB_DATA *key, TDB_DATA *value)
4391 char line [1024]; /* FIXME: make this more flexible? */
4395 ptr = fgets(line, sizeof(line), file);
4402 t = ptrans_parse_string(mem_ctx, line, key);
4403 if (t == NULL || key->dptr == NULL) {
4404 /* Line Ignored but not EOF */
4409 t = ptrans_parse_string(mem_ctx, t, value);
4411 /* Line Ignored but not EOF */
4412 talloc_free(key->dptr);
4421 * Update a persistent database as per file/stdin
4423 static int control_ptrans(struct ctdb_context *ctdb,
4424 int argc, const char **argv)
4426 const char *db_name;
4427 struct ctdb_db_context *ctdb_db;
4428 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4429 struct ctdb_transaction_handle *h;
4430 TDB_DATA key, value;
4435 talloc_free(tmp_ctx);
4441 file = fopen(argv[1], "r");
4443 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4444 talloc_free(tmp_ctx);
4451 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4452 if (ctdb_db == NULL) {
4453 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4457 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4459 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4463 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4464 if (key.dsize != 0) {
4465 ret = ctdb_transaction_store(h, key, value);
4466 /* Minimise memory use */
4467 talloc_free(key.dptr);
4468 if (value.dptr != NULL) {
4469 talloc_free(value.dptr);
4472 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4473 ctdb_transaction_cancel(h);
4479 ret = ctdb_transaction_commit(h);
4481 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4485 if (file != stdin) {
4488 talloc_free(tmp_ctx);
4492 if (file != stdin) {
4496 talloc_free(tmp_ctx);
4501 check if a service is bound to a port or not
4503 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4508 struct sockaddr_in sin;
4511 printf("Use: ctdb chktcport <port>\n");
4515 port = atoi(argv[0]);
4517 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4519 printf("Failed to open local socket\n");
4523 v = fcntl(s, F_GETFL, 0);
4524 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4525 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4528 bzero(&sin, sizeof(sin));
4529 sin.sin_family = PF_INET;
4530 sin.sin_port = htons(port);
4531 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4534 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4542 /* Reload public IPs on a specified nodes */
4543 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4545 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4551 assert_single_node_only();
4557 /* Determine the nodes where IPs need to be reloaded */
4558 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4559 options.pnn, true, &nodes, &pnn_mode)) {
4565 /* Disable takeover runs on all connected nodes. A reply
4566 * indicating success is needed from each node so all nodes
4567 * will need to be active. This will retry until maxruntime
4568 * is exceeded, hence no error handling.
4570 * A check could be added to not allow reloading of IPs when
4571 * there are disconnected nodes. However, this should
4572 * probably be left up to the administrator.
4574 timeout = LONGTIMEOUT;
4575 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4576 "Disable takeover runs", true);
4578 /* Now tell all the desired nodes to reload their public IPs.
4579 * Keep trying this until it succeeds. This assumes all
4580 * failures are transient, which might not be true...
4582 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4583 nodes, 0, LONGTIMELIMIT(),
4585 NULL, NULL, NULL) != 0) {
4587 ("Unable to reload IPs on some nodes, try again.\n"));
4591 /* It isn't strictly necessary to wait until takeover runs are
4592 * re-enabled but doing so can't hurt.
4595 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4596 "Enable takeover runs", true);
4602 talloc_free(tmp_ctx);
4607 display a list of the databases on a remote ctdb
4609 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4612 struct ctdb_dbid_map *dbmap=NULL;
4614 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4616 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4620 if(options.machinereadable){
4621 printm(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4622 for(i=0;i<dbmap->num;i++){
4623 const char *path = NULL;
4624 const char *name = NULL;
4625 const char *health = NULL;
4630 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4631 dbmap->dbs[i].dbid, ctdb, &path);
4632 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4633 dbmap->dbs[i].dbid, ctdb, &name);
4634 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4635 dbmap->dbs[i].dbid, ctdb, &health);
4636 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4637 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4638 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4639 printm(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4640 dbmap->dbs[i].dbid, name, path,
4641 !!(persistent), !!(sticky),
4642 !!(health), !!(readonly));
4647 printf("Number of databases:%d\n", dbmap->num);
4648 for(i=0;i<dbmap->num;i++){
4649 const char *path = NULL;
4650 const char *name = NULL;
4651 const char *health = NULL;
4656 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4657 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4658 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4659 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4660 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4661 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4662 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4663 dbmap->dbs[i].dbid, name, path,
4664 persistent?" PERSISTENT":"",
4665 sticky?" STICKY":"",
4666 readonly?" READONLY":"",
4667 health?" UNHEALTHY":"");
4674 display the status of a database on a remote ctdb
4676 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4678 const char *db_name;
4681 const char *path = NULL;
4682 const char *health = NULL;
4688 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4692 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4693 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4694 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4695 db_id, db_name, path,
4696 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4697 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4698 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4699 (health ? health : "OK"));
4705 check if the local node is recmaster or not
4706 it will return 1 if this node is the recmaster and 0 if it is not
4707 or if the local ctdb daemon could not be contacted
4709 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4711 uint32_t mypnn, recmaster;
4714 assert_single_node_only();
4716 mypnn = getpnn(ctdb);
4718 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4720 printf("Failed to get the recmaster\n");
4724 if (recmaster != mypnn) {
4725 printf("this node is not the recmaster\n");
4729 printf("this node is the recmaster\n");
4736 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4739 struct timeval tv = timeval_current();
4740 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4742 printf("Unable to get ping response from node %u\n", options.pnn);
4745 printf("response from %u time=%.6f sec (%d clients)\n",
4746 options.pnn, timeval_elapsed(&tv), ret);
4753 get a node's runstate
4755 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4758 enum ctdb_runstate runstate;
4760 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4762 printf("Unable to get runstate response from node %u\n",
4767 enum ctdb_runstate t;
4769 for (i=0; i<argc; i++) {
4771 t = runstate_from_string(argv[i]);
4772 if (t == CTDB_RUNSTATE_UNKNOWN) {
4773 printf("Invalid run state (%s)\n", argv[i]);
4777 if (t == runstate) {
4784 printf("CTDB not in required run state (got %s)\n",
4785 runstate_to_string((enum ctdb_runstate)runstate));
4790 printf("%s\n", runstate_to_string(runstate));
4798 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4809 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4811 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4815 printf("%-23s = %u\n", name, value);
4822 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4833 value = strtoul(argv[1], NULL, 0);
4835 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4837 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4841 DEBUG(DEBUG_WARNING,
4842 ("Setting obsolete tunable variable '%s'\n",
4851 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4857 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4859 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4863 for (i=0;i<count;i++) {
4864 control_getvar(ctdb, 1, &list[i]);
4873 display debug level on a node
4875 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4880 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4882 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4885 const char *desc = get_debug_by_level(level);
4887 /* This should never happen */
4890 if (options.machinereadable){
4891 printm(":Name:Level:\n");
4892 printm(":%s:%d:\n", desc, level);
4894 printf("Node %u is at debug level %s (%d)\n",
4895 options.pnn, desc, level);
4902 display reclock file of a node
4904 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4907 const char *reclock;
4909 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4911 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4914 if (options.machinereadable){
4915 if (reclock != NULL) {
4916 printm("%s", reclock);
4919 if (reclock == NULL) {
4920 printf("No reclock file used.\n");
4922 printf("Reclock file:%s\n", reclock);
4930 set the reclock file of a node
4932 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4935 const char *reclock = NULL;
4939 } else if (argc == 1) {
4945 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4947 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4954 set the natgw state on/off
4956 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4959 uint32_t natgwstate = 0;
4965 if (!strcmp(argv[0], "on")) {
4967 } else if (!strcmp(argv[0], "off")) {
4973 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4975 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4983 set the lmaster role on/off
4985 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4988 uint32_t lmasterrole = 0;
4994 if (!strcmp(argv[0], "on")) {
4996 } else if (!strcmp(argv[0], "off")) {
5002 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
5004 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
5012 set the recmaster role on/off
5014 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5017 uint32_t recmasterrole = 0;
5023 if (!strcmp(argv[0], "on")) {
5025 } else if (!strcmp(argv[0], "off")) {
5031 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5033 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5041 set debug level on a node or all nodes
5043 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5049 printf("You must specify the debug level. Valid levels are:\n");
5050 print_debug_levels(stdout);
5054 if (!parse_debug(argv[0], &level)) {
5055 printf("Invalid debug level, must be one of\n");
5056 print_debug_levels(stdout);
5060 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5062 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5071 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5077 priority = strtol(argv[0], NULL, 0);
5081 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5083 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5085 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5092 attach to a database
5094 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5096 const char *db_name;
5097 struct ctdb_db_context *ctdb_db;
5098 bool persistent = false;
5108 if (strcmp(argv[1], "persistent") != 0) {
5114 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5115 if (ctdb_db == NULL) {
5116 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5124 * detach from a database
5126 static int control_detach(struct ctdb_context *ctdb, int argc,
5131 int ret, i, status = 0;
5132 struct ctdb_node_map *nodemap = NULL;
5133 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5140 assert_single_node_only();
5142 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5145 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5146 "when recovery is active\n"));
5147 talloc_free(tmp_ctx);
5151 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5154 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5156 talloc_free(tmp_ctx);
5160 for (i=0; i<nodemap->num; i++) {
5163 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5167 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5171 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5172 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5173 "inactive (stopped or banned) node "
5174 "%u\n", nodemap->nodes[i].pnn));
5175 talloc_free(tmp_ctx);
5179 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5180 nodemap->nodes[i].pnn,
5181 "AllowClientDBAttach",
5184 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5185 "AllowClientDBAttach from node %u\n",
5186 nodemap->nodes[i].pnn));
5187 talloc_free(tmp_ctx);
5192 DEBUG(DEBUG_ERR, ("Database access is still active on "
5193 "node %u. Set AllowClientDBAttach=0 "
5195 nodemap->nodes[i].pnn));
5196 talloc_free(tmp_ctx);
5201 talloc_free(tmp_ctx);
5203 for (i=0; i<argc; i++) {
5204 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5208 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5209 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5210 "cannot be detached\n", argv[i]));
5215 ret = ctdb_detach(ctdb, db_id);
5217 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5229 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5231 struct ctdb_db_priority db_prio;
5238 db_prio.db_id = strtoul(argv[0], NULL, 0);
5239 db_prio.priority = strtoul(argv[1], NULL, 0);
5241 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5243 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5253 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5255 uint32_t db_id, priority;
5262 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5266 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5268 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5272 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5278 set the sticky records capability for a database
5280 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5282 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5290 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5294 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5296 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5297 talloc_free(tmp_ctx);
5301 talloc_free(tmp_ctx);
5306 set the readonly capability for a database
5308 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5310 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5318 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5322 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5324 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5325 talloc_free(tmp_ctx);
5329 talloc_free(tmp_ctx);
5336 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5346 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5350 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5352 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5356 printf("Sequence number:%lld\n", (long long)seqnum);
5362 run an eventscript on a node
5364 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5370 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5373 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5377 data.dptr = (unsigned char *)discard_const(argv[0]);
5378 data.dsize = strlen((char *)data.dptr) + 1;
5380 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5382 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5383 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5384 if (ret != 0 || res != 0) {
5385 if (errmsg != NULL) {
5387 ("Failed to run eventscripts - %s\n", errmsg));
5389 DEBUG(DEBUG_ERR, ("Failed to run eventscripts\n"));
5391 talloc_free(tmp_ctx);
5394 talloc_free(tmp_ctx);
5398 #define DB_VERSION 1
5399 #define MAX_DB_NAME 64
5400 struct db_file_header {
5401 unsigned long version;
5403 unsigned long persistent;
5405 const char name[MAX_DB_NAME];
5408 struct backup_data {
5409 struct ctdb_marshall_buffer *records;
5412 bool traverse_error;
5415 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5417 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5418 struct ctdb_rec_data *rec;
5420 /* add the record */
5421 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5423 bd->traverse_error = true;
5424 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5427 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5428 if (bd->records == NULL) {
5429 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5430 bd->traverse_error = true;
5433 bd->records->count++;
5434 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5435 bd->len += rec->length;
5443 * backup a database to a file
5445 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5447 const char *db_name;
5449 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5450 struct db_file_header dbhdr;
5451 struct ctdb_db_context *ctdb_db;
5452 struct backup_data *bd;
5455 const char *reason = NULL;
5459 assert_single_node_only();
5462 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5466 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5470 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5471 db_id, tmp_ctx, &reason);
5473 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5475 talloc_free(tmp_ctx);
5479 uint32_t allow_unhealthy = 0;
5481 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5482 "AllowUnhealthyDBRead",
5485 if (allow_unhealthy != 1) {
5486 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5489 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5491 talloc_free(tmp_ctx);
5495 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5497 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5498 "tunnable AllowUnhealthyDBRead = %u\n",
5502 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5503 if (ctdb_db == NULL) {
5504 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5505 talloc_free(tmp_ctx);
5510 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5512 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5513 talloc_free(tmp_ctx);
5518 bd = talloc_zero(tmp_ctx, struct backup_data);
5520 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5521 talloc_free(tmp_ctx);
5525 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5526 if (bd->records == NULL) {
5527 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5528 talloc_free(tmp_ctx);
5532 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5533 bd->records->db_id = ctdb_db->db_id;
5534 /* traverse the database collecting all records */
5535 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5536 bd->traverse_error) {
5537 DEBUG(DEBUG_ERR,("Traverse error\n"));
5538 talloc_free(tmp_ctx);
5542 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5545 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5547 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5548 talloc_free(tmp_ctx);
5553 dbhdr.version = DB_VERSION;
5554 dbhdr.timestamp = time(NULL);
5555 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5556 dbhdr.size = bd->len;
5557 if (strlen(argv[0]) >= MAX_DB_NAME) {
5558 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5561 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5562 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
5564 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5567 ret = sys_write(fh, bd->records, bd->len);
5569 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5578 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5582 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5584 talloc_free(tmp_ctx);
5589 * restore a database from a file
5591 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5594 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5597 struct db_file_header dbhdr;
5598 struct ctdb_db_context *ctdb_db;
5599 struct ctdb_node_map *nodemap=NULL;
5600 struct ctdb_vnn_map *vnnmap=NULL;
5602 struct ctdb_control_transdb w;
5604 uint32_t generation;
5609 assert_single_node_only();
5611 if (argc < 1 || argc > 2) {
5612 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5616 fh = open(argv[0], O_RDONLY);
5618 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5619 talloc_free(tmp_ctx);
5623 sys_read(fh, &dbhdr, sizeof(dbhdr));
5624 if (dbhdr.version != DB_VERSION) {
5625 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5627 talloc_free(tmp_ctx);
5631 dbname = discard_const(dbhdr.name);
5633 dbname = discard_const(argv[1]);
5636 outdata.dsize = dbhdr.size;
5637 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5638 if (outdata.dptr == NULL) {
5639 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5641 talloc_free(tmp_ctx);
5644 sys_read(fh, outdata.dptr, outdata.dsize);
5647 tm = localtime(&dbhdr.timestamp);
5648 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5649 printf("Restoring database '%s' from backup @ %s\n",
5653 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5654 if (ctdb_db == NULL) {
5655 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5656 talloc_free(tmp_ctx);
5660 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5662 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5663 talloc_free(tmp_ctx);
5668 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5670 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5671 talloc_free(tmp_ctx);
5675 /* freeze all nodes */
5676 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5677 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5678 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5684 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5685 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5686 talloc_free(tmp_ctx);
5691 generation = vnnmap->generation;
5692 data.dptr = (void *)&generation;
5693 data.dsize = sizeof(generation);
5695 /* start a cluster wide transaction */
5696 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5697 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5699 TIMELIMIT(), false, data,
5702 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5707 w.db_id = ctdb_db->db_id;
5708 w.transaction_id = generation;
5710 data.dptr = (void *)&w;
5711 data.dsize = sizeof(w);
5713 /* wipe all the remote databases. */
5714 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5715 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5717 TIMELIMIT(), false, data,
5720 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5721 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5722 talloc_free(tmp_ctx);
5726 /* push the database */
5727 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5728 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5730 TIMELIMIT(), false, outdata,
5733 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5734 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5735 talloc_free(tmp_ctx);
5739 data.dptr = (void *)&ctdb_db->db_id;
5740 data.dsize = sizeof(ctdb_db->db_id);
5742 /* mark the database as healthy */
5743 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5744 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5746 TIMELIMIT(), false, data,
5749 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5750 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5751 talloc_free(tmp_ctx);
5755 data.dptr = (void *)&generation;
5756 data.dsize = sizeof(generation);
5758 /* commit all the changes */
5759 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5761 TIMELIMIT(), false, data,
5764 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5765 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5766 talloc_free(tmp_ctx);
5771 /* thaw all nodes */
5772 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5773 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5779 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5780 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5781 talloc_free(tmp_ctx);
5786 talloc_free(tmp_ctx);
5791 * dump a database backup from a file
5793 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5795 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5797 struct db_file_header dbhdr;
5801 struct ctdb_rec_data *rec = NULL;
5802 struct ctdb_marshall_buffer *m;
5803 struct ctdb_dump_db_context c;
5805 assert_single_node_only();
5808 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5812 fh = open(argv[0], O_RDONLY);
5814 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5815 talloc_free(tmp_ctx);
5819 sys_read(fh, &dbhdr, sizeof(dbhdr));
5820 if (dbhdr.version != DB_VERSION) {
5821 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5823 talloc_free(tmp_ctx);
5827 outdata.dsize = dbhdr.size;
5828 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5829 if (outdata.dptr == NULL) {
5830 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5832 talloc_free(tmp_ctx);
5835 sys_read(fh, outdata.dptr, outdata.dsize);
5837 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5839 tm = localtime(&dbhdr.timestamp);
5840 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5841 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5842 dbhdr.name, m->db_id, tbuf);
5847 c.printemptyrecords = (bool)options.printemptyrecords;
5848 c.printdatasize = (bool)options.printdatasize;
5849 c.printlmaster = false;
5850 c.printhash = (bool)options.printhash;
5851 c.printrecordflags = (bool)options.printrecordflags;
5853 for (i=0; i < m->count; i++) {
5857 /* we do not want the header splitted, so we pass NULL*/
5858 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5861 ctdb_dumpdb_record(key, data, &c);
5864 printf("Dumped %d records\n", i);
5865 talloc_free(tmp_ctx);
5870 * wipe a database from a file
5872 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5875 const char *db_name;
5877 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5879 struct ctdb_db_context *ctdb_db;
5880 struct ctdb_node_map *nodemap = NULL;
5881 struct ctdb_vnn_map *vnnmap = NULL;
5883 struct ctdb_control_transdb w;
5885 uint32_t generation;
5888 assert_single_node_only();
5891 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5895 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5899 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5900 if (ctdb_db == NULL) {
5901 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5903 talloc_free(tmp_ctx);
5907 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5910 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5912 talloc_free(tmp_ctx);
5916 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5919 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5921 talloc_free(tmp_ctx);
5925 /* freeze all nodes */
5926 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5927 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5928 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5935 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5936 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5937 CTDB_RECOVERY_ACTIVE);
5938 talloc_free(tmp_ctx);
5943 generation = vnnmap->generation;
5944 data.dptr = (void *)&generation;
5945 data.dsize = sizeof(generation);
5947 /* start a cluster wide transaction */
5948 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5949 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5951 TIMELIMIT(), false, data,
5955 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5956 "transactions.\n"));
5960 w.db_id = ctdb_db->db_id;
5961 w.transaction_id = generation;
5963 data.dptr = (void *)&w;
5964 data.dsize = sizeof(w);
5966 /* wipe all the remote databases. */
5967 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5968 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5970 TIMELIMIT(), false, data,
5973 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5974 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5975 talloc_free(tmp_ctx);
5979 data.dptr = (void *)&ctdb_db->db_id;
5980 data.dsize = sizeof(ctdb_db->db_id);
5982 /* mark the database as healthy */
5983 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5984 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5986 TIMELIMIT(), false, data,
5989 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5990 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5991 talloc_free(tmp_ctx);
5995 data.dptr = (void *)&generation;
5996 data.dsize = sizeof(generation);
5998 /* commit all the changes */
5999 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
6001 TIMELIMIT(), false, data,
6004 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
6005 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6006 talloc_free(tmp_ctx);
6010 /* thaw all nodes */
6011 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6012 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
6018 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
6019 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6020 talloc_free(tmp_ctx);
6024 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
6026 talloc_free(tmp_ctx);
6033 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6039 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6040 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6041 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6042 if (ret != 0 || res != 0) {
6043 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6044 talloc_free(tmp_ctx);
6047 sys_write(1, data.dptr, data.dsize);
6048 talloc_free(tmp_ctx);
6053 handler for memory dumps
6055 static void mem_dump_handler(uint64_t srvid, TDB_DATA data, void *private_data)
6057 sys_write(1, data.dptr, data.dsize);
6062 dump memory usage on the recovery daemon
6064 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6068 struct srvid_request rd;
6070 rd.pnn = ctdb_get_pnn(ctdb);
6071 rd.srvid = getpid();
6073 /* register a message port for receiveing the reply so that we
6074 can receive the reply
6076 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6079 data.dptr = (uint8_t *)&rd;
6080 data.dsize = sizeof(rd);
6082 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6084 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6088 /* this loop will terminate when we have received the reply */
6090 tevent_loop_once(ctdb->ev);
6097 send a message to a srvid
6099 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6101 unsigned long srvid;
6109 srvid = strtoul(argv[0], NULL, 0);
6111 data.dptr = (uint8_t *)discard_const(argv[1]);
6112 data.dsize= strlen(argv[1]);
6114 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6116 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6124 handler for msglisten
6126 static void msglisten_handler(uint64_t srvid, TDB_DATA data,
6131 printf("Message received: ");
6132 for (i=0;i<data.dsize;i++) {
6133 printf("%c", data.dptr[i]);
6139 listen for messages on a messageport
6141 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6147 /* register a message port and listen for messages
6149 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6150 printf("Listening for messages on srvid:%d\n", (int)srvid);
6153 tevent_loop_once(ctdb->ev);
6160 list all nodes in the cluster
6161 we parse the nodes file directly
6163 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6165 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6166 struct ctdb_node_map *node_map;
6169 assert_single_node_only();
6171 node_map = read_nodes_file(mem_ctx);
6172 if (node_map == NULL) {
6173 talloc_free(mem_ctx);
6177 for (i = 0; i < node_map->num; i++) {
6180 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
6183 addr = ctdb_addr_to_str(&node_map->nodes[i].addr);
6184 if (options.machinereadable){
6185 printm(":%d:%s:\n", node_map->nodes[i].pnn, addr);
6187 printf("%s\n", addr);
6190 talloc_free(mem_ctx);
6195 /**********************************************************************/
6196 /* reload the nodes file on all nodes */
6198 static void get_nodes_files_callback(struct ctdb_context *ctdb,
6199 uint32_t node_pnn, int32_t res,
6200 TDB_DATA outdata, void *callback_data)
6202 struct ctdb_node_map **maps =
6203 talloc_get_type(callback_data, struct ctdb_node_map *);
6205 if (outdata.dsize < offsetof(struct ctdb_node_map, nodes) ||
6206 outdata.dptr == NULL) {
6208 (__location__ " Invalid return data: %u %p\n",
6209 (unsigned)outdata.dsize, outdata.dptr));
6213 if (node_pnn >= talloc_array_length(maps)) {
6215 (__location__ " unexpected PNN %u\n", node_pnn));
6219 maps[node_pnn] = talloc_memdup(maps, outdata.dptr, outdata.dsize);
6222 static void get_nodes_files_fail_callback(struct ctdb_context *ctdb,
6223 uint32_t node_pnn, int32_t res,
6224 TDB_DATA outdata, void *callback_data)
6227 ("ERROR: Failed to get nodes file from node %u\n", node_pnn));
6230 static struct ctdb_node_map **
6231 ctdb_get_nodes_files(struct ctdb_context *ctdb,
6232 TALLOC_CTX *mem_ctx,
6233 struct timeval timeout,
6234 struct ctdb_node_map *nodemap)
6238 struct ctdb_node_map **maps;
6240 maps = talloc_zero_array(mem_ctx, struct ctdb_node_map *, nodemap->num);
6241 CTDB_NO_MEMORY_NULL(ctdb, maps);
6243 nodes = list_of_connected_nodes(ctdb, nodemap, mem_ctx, true);
6245 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_NODES_FILE,
6246 nodes, 0, TIMELIMIT(),
6248 get_nodes_files_callback,
6249 get_nodes_files_fail_callback,
6259 static bool node_files_are_identical(struct ctdb_node_map *nm1,
6260 struct ctdb_node_map *nm2)
6264 if (nm1->num != nm2->num) {
6267 for (i = 0; i < nm1->num; i++) {
6268 if (memcmp(&nm1->nodes[i], &nm2->nodes[i],
6269 sizeof(struct ctdb_node_and_flags)) != 0) {
6277 static bool check_all_node_files_are_identical(struct ctdb_context *ctdb,
6278 TALLOC_CTX *mem_ctx,
6279 struct timeval timeout,
6280 struct ctdb_node_map *nodemap,
6281 struct ctdb_node_map *file_nodemap)
6283 static struct ctdb_node_map **maps;
6287 maps = ctdb_get_nodes_files(ctdb, mem_ctx, timeout, nodemap);
6292 for (i = 0; i < talloc_array_length(maps); i++) {
6293 if (maps[i] == NULL) {
6296 if (!node_files_are_identical(file_nodemap, maps[i])) {
6298 ("ERROR: Node file on node %u differs from current node (%u)\n",
6299 i, ctdb_get_pnn(ctdb)));
6308 reload the nodes file on the local node
6310 static bool sanity_check_nodes_file_changes(TALLOC_CTX *mem_ctx,
6311 struct ctdb_node_map *nodemap,
6312 struct ctdb_node_map *file_nodemap)
6315 bool should_abort = false;
6316 bool have_changes = false;
6318 for (i=0; i<nodemap->num; i++) {
6319 if (i >= file_nodemap->num) {
6321 ("ERROR: Node %u (%s) missing from nodes file\n",
6322 nodemap->nodes[i].pnn,
6323 ctdb_addr_to_str(&nodemap->nodes[i].addr)));
6324 should_abort = true;
6327 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED &&
6328 file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6329 /* Node remains deleted */
6331 ("Node %u is unchanged (DELETED)\n",
6332 nodemap->nodes[i].pnn));
6333 } else if (!(nodemap->nodes[i].flags & NODE_FLAGS_DELETED) &&
6334 !(file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED)) {
6335 /* Node not newly nor previously deleted */
6336 if (!ctdb_same_ip(&nodemap->nodes[i].addr,
6337 &file_nodemap->nodes[i].addr)) {
6339 ("ERROR: Node %u has changed IP address (was %s, now %s)\n",
6340 nodemap->nodes[i].pnn,
6341 /* ctdb_addr_to_str() returns a static */
6342 talloc_strdup(mem_ctx,
6343 ctdb_addr_to_str(&nodemap->nodes[i].addr)),
6344 ctdb_addr_to_str(&file_nodemap->nodes[i].addr)));
6345 should_abort = true;
6348 ("Node %u is unchanged\n",
6349 nodemap->nodes[i].pnn));
6350 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
6351 DEBUG(DEBUG_WARNING,
6352 ("WARNING: Node %u is disconnected. You MUST fix this node manually!\n",
6353 nodemap->nodes[i].pnn));
6356 } else if (file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6357 /* Node is being deleted */
6359 ("Node %u is DELETED\n",
6360 nodemap->nodes[i].pnn));
6361 have_changes = true;
6362 if (!(nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED)) {
6364 ("ERROR: Node %u is still connected\n",
6365 nodemap->nodes[i].pnn));
6366 should_abort = true;
6368 } else if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6369 /* Node was previously deleted */
6371 ("Node %u is UNDELETED\n", nodemap->nodes[i].pnn));
6372 have_changes = true;
6378 ("ERROR: Nodes will not be reloaded due to previous error\n"));
6379 talloc_free(mem_ctx);
6383 /* Leftover nodes in file are NEW */
6384 for (; i < file_nodemap->num; i++) {
6385 DEBUG(DEBUG_NOTICE, ("Node %u is NEW\n",
6386 file_nodemap->nodes[i].pnn));
6387 have_changes = true;
6390 return have_changes;
6393 static void reload_nodes_fail_callback(struct ctdb_context *ctdb,
6394 uint32_t node_pnn, int32_t res,
6395 TDB_DATA outdata, void *callback_data)
6397 DEBUG(DEBUG_WARNING,
6398 ("WARNING: Node %u failed to reload nodes. You MUST fix this node manually!\n",
6402 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6405 struct ctdb_node_map *nodemap=NULL;
6406 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
6407 struct ctdb_node_map *file_nodemap;
6411 assert_current_node_only(ctdb);
6413 /* Load both the current nodemap and the contents of the local
6414 * nodes file. Compare and sanity check them before doing
6417 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6419 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6423 file_nodemap = read_nodes_file(tmp_ctx);
6424 if (file_nodemap == NULL) {
6425 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6426 talloc_free(tmp_ctx);
6430 if (!check_all_node_files_are_identical(ctdb, tmp_ctx, TIMELIMIT(),
6431 nodemap, file_nodemap)) {
6435 if (!sanity_check_nodes_file_changes(tmp_ctx, nodemap, file_nodemap)) {
6437 ("No change in nodes file, skipping unnecessary reload\n"));
6438 talloc_free(tmp_ctx);
6442 /* Now make the changes */
6443 conn = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
6444 for (i = 0; i < talloc_array_length(conn); i++) {
6445 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n",
6449 /* Another timeout could be used, such as ReRecoveryTimeout or
6450 * a new one for this purpose. However, this is the simplest
6452 timeout = options.timelimit;
6453 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
6454 "Disable recoveries", true);
6457 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_NODES_FILE,
6458 conn, 0, TIMELIMIT(),
6460 NULL, reload_nodes_fail_callback,
6464 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
6465 "Enable recoveries", true);
6467 talloc_free(tmp_ctx);
6473 static const struct {
6475 int (*fn)(struct ctdb_context *, int, const char **);
6477 bool without_daemon; /* can be run without daemon running ? */
6480 } ctdb_commands[] = {
6481 { "version", control_version, true, true, "show version of ctdb" },
6482 { "status", control_status, true, false, "show node status" },
6483 { "uptime", control_uptime, true, false, "show node uptime" },
6484 { "ping", control_ping, true, false, "ping all nodes" },
6485 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6486 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6487 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6488 { "listvars", control_listvars, true, false, "list tunable variables"},
6489 { "statistics", control_statistics, false, false, "show statistics" },
6490 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6491 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6492 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6493 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6494 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6495 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6496 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6497 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6498 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6499 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6500 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6501 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6502 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6503 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6504 { "lvs", control_lvs, true, false, "show lvs configuration" },
6505 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6506 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6507 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6508 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6509 { "getdebug", control_getdebug, true, false, "get debug level" },
6510 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6511 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6512 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6513 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6514 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6515 { "disable", control_disable, true, false, "disable a nodes public IP" },
6516 { "enable", control_enable, true, false, "enable a nodes public IP" },
6517 { "stop", control_stop, true, false, "stop a node" },
6518 { "continue", control_continue, true, false, "re-start a stopped node" },
6519 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6520 { "unban", control_unban, true, false, "unban a node" },
6521 { "showban", control_showban, true, false, "show ban information"},
6522 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6523 { "recover", control_recover, true, false, "force recovery" },
6524 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6525 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6526 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6527 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6528 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6529 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6530 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6531 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6532 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6534 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6536 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6537 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6538 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6539 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6540 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6541 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6542 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6543 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6544 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6545 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6546 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6547 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6548 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6549 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6550 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6551 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6552 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6553 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6554 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6555 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6556 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6557 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6558 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6559 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6560 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6561 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6562 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6563 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6564 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6565 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6566 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6567 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6568 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6569 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6570 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6571 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6572 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6573 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6574 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6575 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6576 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6577 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6578 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6579 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6580 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6581 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6582 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6583 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6589 static void usage(void)
6593 "Usage: ctdb [options] <control>\n" \
6595 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6596 " -Y generate machine readable output\n"
6597 " -x <char> specify delimiter for machine readable output\n"
6598 " -v generate verbose output\n"
6599 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6600 printf("Controls:\n");
6601 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6602 printf(" %-15s %-27s %s\n",
6603 ctdb_commands[i].name,
6604 ctdb_commands[i].args?ctdb_commands[i].args:"",
6605 ctdb_commands[i].msg);
6611 static void ctdb_alarm(int sig)
6613 printf("Maximum runtime exceeded - exiting\n");
6620 int main(int argc, const char *argv[])
6622 struct ctdb_context *ctdb;
6623 char *nodestring = NULL;
6624 int machineparsable = 0;
6625 struct poptOption popt_options[] = {
6628 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6629 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6630 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machine readable output", NULL },
6631 { NULL, 'x', POPT_ARG_STRING, &options.machineseparator, 0, "specify separator for machine readable output", "char" },
6632 { NULL, 'X', POPT_ARG_NONE, &machineparsable, 0, "enable machine parsable output with separator |", NULL },
6633 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6634 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6635 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6636 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6637 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6638 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6639 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6643 const char **extra_argv;
6647 struct tevent_context *ev;
6648 const char *control;
6652 /* set some defaults */
6653 options.maxruntime = 0;
6654 options.timelimit = 10;
6655 options.pnn = CTDB_CURRENT_NODE;
6657 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6659 while ((opt = poptGetNextOpt(pc)) != -1) {
6662 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6663 poptBadOption(pc, 0), poptStrerror(opt)));
6668 /* setup the remaining options for the main program to use */
6669 extra_argv = poptGetArgs(pc);
6672 while (extra_argv[extra_argc]) extra_argc++;
6675 if (extra_argc < 1) {
6679 if (options.maxruntime == 0) {
6680 const char *ctdb_timeout;
6681 ctdb_timeout = getenv("CTDB_TIMEOUT");
6682 if (ctdb_timeout != NULL) {
6683 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6685 /* default timeout is 120 seconds */
6686 options.maxruntime = 120;
6690 if (machineparsable) {
6691 options.machineseparator = "|";
6693 if (options.machineseparator != NULL) {
6694 if (strlen(options.machineseparator) != 1) {
6695 printf("Invalid separator \"%s\" - "
6696 "must be single character\n",
6697 options.machineseparator);
6702 options.machinereadable = true;
6703 } else if (options.machinereadable) {
6704 options.machineseparator = ":";
6707 signal(SIGALRM, ctdb_alarm);
6708 alarm(options.maxruntime);
6710 control = extra_argv[0];
6712 /* Default value for CTDB_BASE - don't override */
6713 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6715 ev = tevent_context_init(NULL);
6717 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6721 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6722 if (strcmp(control, ctdb_commands[i].name) == 0) {
6727 if (i == ARRAY_SIZE(ctdb_commands)) {
6728 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6732 if (ctdb_commands[i].without_daemon == true) {
6733 if (nodestring != NULL) {
6734 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6737 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6740 /* initialise ctdb */
6741 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6745 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6747 pnn = find_node_xpnn();
6750 ("Is this node part of a CTDB cluster?\n"));
6755 /* setup the node number(s) to contact */
6756 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6757 &options.nodes, &options.pnn)) {
6761 if (options.pnn == CTDB_CURRENT_NODE) {
6762 options.pnn = options.nodes[0];
6765 if (ctdb_commands[i].auto_all &&
6766 ((options.pnn == CTDB_BROADCAST_ALL) ||
6767 (options.pnn == CTDB_MULTICAST))) {
6771 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6772 options.pnn = options.nodes[j];
6773 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6776 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6781 (void)poptFreeContext(pc);