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"
28 #include "../include/ctdb_version.h"
29 #include "../include/ctdb_client.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
32 #include "lib/tdb_wrap/tdb_wrap.h"
33 #include "lib/util/dlinklist.h"
35 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
36 #define ERR_NONODE 21 /* node does not exist */
37 #define ERR_DISNODE 22 /* node is disconnected */
39 static void usage(void);
46 const char *machineseparator;
49 int printemptyrecords;
56 #define LONGTIMEOUT options.timelimit*10
58 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
59 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
61 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
63 return (tv2->tv_sec - tv->tv_sec) +
64 (tv2->tv_usec - tv->tv_usec)*1.0e-6;
67 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
69 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
73 /* Like printf(3) but substitute for separator in format */
74 static int printm(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
75 static int printm(const char *format, ...)
79 size_t len = strlen(format);
80 char new_format[len+1];
82 strcpy(new_format, format);
84 if (options.machineseparator[0] != ':') {
85 all_string_sub(new_format,
86 ":", options.machineseparator, len + 1);
90 ret = vprintf(new_format, ap);
96 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
97 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
98 "Out of memory in " __location__ )); \
102 static uint32_t getpnn(struct ctdb_context *ctdb)
104 if ((options.pnn == CTDB_BROADCAST_ALL) ||
105 (options.pnn == CTDB_MULTICAST)) {
107 ("Cannot get PNN for node %u\n", options.pnn));
111 if (options.pnn == CTDB_CURRENT_NODE) {
112 return ctdb_get_pnn(ctdb);
118 static void assert_single_node_only(void)
120 if ((options.pnn == CTDB_BROADCAST_ALL) ||
121 (options.pnn == CTDB_MULTICAST)) {
123 ("This control can not be applied to multiple PNNs\n"));
128 /* Pretty print the flags to a static buffer in human-readable format.
129 * This never returns NULL!
131 static const char *pretty_print_flags(uint32_t flags)
134 static const struct {
138 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
139 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
140 { NODE_FLAGS_BANNED, "BANNED" },
141 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
142 { NODE_FLAGS_DELETED, "DELETED" },
143 { NODE_FLAGS_STOPPED, "STOPPED" },
144 { NODE_FLAGS_INACTIVE, "INACTIVE" },
146 static char flags_str[512]; /* Big enough to contain all flag names */
149 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
150 if (flags & flag_names[j].flag) {
151 if (flags_str[0] == '\0') {
152 (void) strcpy(flags_str, flag_names[j].name);
154 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
155 (void) strncat(flags_str, flag_names[j].name,
156 sizeof(flags_str)-1);
160 if (flags_str[0] == '\0') {
161 (void) strcpy(flags_str, "OK");
167 static int h2i(char h)
169 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
170 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
174 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
177 TDB_DATA key = {NULL, 0};
181 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
186 key.dptr = talloc_size(mem_ctx, key.dsize);
188 for (i=0; i < len/2; i++) {
189 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
194 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
195 * that are disconnected or deleted. If dd_ok is true those nodes are
196 * included in the output list of nodes. If dd_ok is false, those
197 * nodes are filtered from the "all" case and cause an error if
198 * explicitly specified.
200 static bool parse_nodestring(struct ctdb_context *ctdb,
202 const char * nodestring,
203 uint32_t current_pnn,
208 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
211 struct ctdb_node_map *nodemap;
216 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
218 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
219 talloc_free(tmp_ctx);
223 if (nodestring != NULL) {
224 *nodes = talloc_array(mem_ctx, uint32_t, 0);
225 if (*nodes == NULL) {
231 if (strcmp(nodestring, "all") == 0) {
232 *pnn_mode = CTDB_BROADCAST_ALL;
235 for (i = 0; i < nodemap->num; i++) {
236 if ((nodemap->nodes[i].flags &
237 (NODE_FLAGS_DISCONNECTED |
238 NODE_FLAGS_DELETED)) && !dd_ok) {
241 *nodes = talloc_realloc(mem_ctx, *nodes,
243 if (*nodes == NULL) {
253 ns = talloc_strdup(tmp_ctx, nodestring);
254 tok = strtok(ns, ",");
255 while (tok != NULL) {
258 i = (uint32_t)strtoul(tok, &endptr, 0);
259 if (i == 0 && tok == endptr) {
261 ("Invalid node %s\n", tok));
262 talloc_free(tmp_ctx);
265 if (i >= nodemap->num) {
266 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
267 talloc_free(tmp_ctx);
270 if ((nodemap->nodes[i].flags &
271 (NODE_FLAGS_DISCONNECTED |
272 NODE_FLAGS_DELETED)) && !dd_ok) {
273 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
274 talloc_free(tmp_ctx);
277 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
278 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
279 talloc_free(tmp_ctx);
283 *nodes = talloc_realloc(mem_ctx, *nodes,
285 if (*nodes == NULL) {
292 tok = strtok(NULL, ",");
297 *pnn_mode = (*nodes)[0];
299 *pnn_mode = CTDB_MULTICAST;
303 /* default - no nodes specified */
304 *nodes = talloc_array(mem_ctx, uint32_t, 1);
305 if (*nodes == NULL) {
308 *pnn_mode = CTDB_CURRENT_NODE;
310 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
315 talloc_free(tmp_ctx);
319 talloc_free(tmp_ctx);
324 check if a database exists
326 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
327 uint32_t *dbid, const char **dbname, uint8_t *flags)
330 struct ctdb_dbid_map *dbmap=NULL;
331 bool dbid_given = false, found = false;
333 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
336 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
338 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
342 if (strncmp(dbarg, "0x", 2) == 0) {
343 id = strtoul(dbarg, NULL, 0);
347 for(i=0; i<dbmap->num; i++) {
349 if (id == dbmap->dbs[i].dbid) {
354 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
356 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
360 if (strcmp(name, dbarg) == 0) {
361 id = dbmap->dbs[i].dbid;
368 if (found && dbid_given && dbname != NULL) {
369 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
371 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
378 if (dbid) *dbid = id;
379 if (dbname) *dbname = talloc_strdup(ctdb, name);
380 if (flags) *flags = dbmap->dbs[i].flags;
382 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
386 talloc_free(tmp_ctx);
391 see if a process exists
393 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
401 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
402 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
406 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
408 printf("%u:%u exists\n", pnn, pid);
410 printf("%u:%u does not exist\n", pnn, pid);
416 display statistics structure
418 static void show_statistics(struct ctdb_statistics *s, int show_header)
420 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
422 const char *prefix=NULL;
424 int tmp, days, hours, minutes, seconds;
429 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
430 STATISTICS_FIELD(num_clients),
431 STATISTICS_FIELD(frozen),
432 STATISTICS_FIELD(recovering),
433 STATISTICS_FIELD(num_recoveries),
434 STATISTICS_FIELD(client_packets_sent),
435 STATISTICS_FIELD(client_packets_recv),
436 STATISTICS_FIELD(node_packets_sent),
437 STATISTICS_FIELD(node_packets_recv),
438 STATISTICS_FIELD(keepalive_packets_sent),
439 STATISTICS_FIELD(keepalive_packets_recv),
440 STATISTICS_FIELD(node.req_call),
441 STATISTICS_FIELD(node.reply_call),
442 STATISTICS_FIELD(node.req_dmaster),
443 STATISTICS_FIELD(node.reply_dmaster),
444 STATISTICS_FIELD(node.reply_error),
445 STATISTICS_FIELD(node.req_message),
446 STATISTICS_FIELD(node.req_control),
447 STATISTICS_FIELD(node.reply_control),
448 STATISTICS_FIELD(client.req_call),
449 STATISTICS_FIELD(client.req_message),
450 STATISTICS_FIELD(client.req_control),
451 STATISTICS_FIELD(timeouts.call),
452 STATISTICS_FIELD(timeouts.control),
453 STATISTICS_FIELD(timeouts.traverse),
454 STATISTICS_FIELD(locks.num_calls),
455 STATISTICS_FIELD(locks.num_current),
456 STATISTICS_FIELD(locks.num_pending),
457 STATISTICS_FIELD(locks.num_failed),
458 STATISTICS_FIELD(total_calls),
459 STATISTICS_FIELD(pending_calls),
460 STATISTICS_FIELD(childwrite_calls),
461 STATISTICS_FIELD(pending_childwrite_calls),
462 STATISTICS_FIELD(memory_used),
463 STATISTICS_FIELD(max_hop_count),
464 STATISTICS_FIELD(total_ro_delegations),
465 STATISTICS_FIELD(total_ro_revokes),
468 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
477 if (options.machinereadable){
479 printm("CTDB version:");
480 printm("Current time of statistics:");
481 printm("Statistics collected since:");
482 for (i=0;i<ARRAY_SIZE(fields);i++) {
483 printm("%s:", fields[i].name);
485 printm("num_reclock_ctdbd_latency:");
486 printm("min_reclock_ctdbd_latency:");
487 printm("avg_reclock_ctdbd_latency:");
488 printm("max_reclock_ctdbd_latency:");
490 printm("num_reclock_recd_latency:");
491 printm("min_reclock_recd_latency:");
492 printm("avg_reclock_recd_latency:");
493 printm("max_reclock_recd_latency:");
495 printm("num_call_latency:");
496 printm("min_call_latency:");
497 printm("avg_call_latency:");
498 printm("max_call_latency:");
500 printm("num_lockwait_latency:");
501 printm("min_lockwait_latency:");
502 printm("avg_lockwait_latency:");
503 printm("max_lockwait_latency:");
505 printm("num_childwrite_latency:");
506 printm("min_childwrite_latency:");
507 printm("avg_childwrite_latency:");
508 printm("max_childwrite_latency:");
511 printm("%d:", CTDB_PROTOCOL);
512 printm("%d:", (int)s->statistics_current_time.tv_sec);
513 printm("%d:", (int)s->statistics_start_time.tv_sec);
514 for (i=0;i<ARRAY_SIZE(fields);i++) {
515 printm("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
517 printm("%d:", s->reclock.ctdbd.num);
518 printm("%.6f:", s->reclock.ctdbd.min);
519 printm("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
520 printm("%.6f:", s->reclock.ctdbd.max);
522 printm("%d:", s->reclock.recd.num);
523 printm("%.6f:", s->reclock.recd.min);
524 printm("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
525 printm("%.6f:", s->reclock.recd.max);
527 printm("%d:", s->call_latency.num);
528 printm("%.6f:", s->call_latency.min);
529 printm("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
530 printm("%.6f:", s->call_latency.max);
532 printm("%d:", s->childwrite_latency.num);
533 printm("%.6f:", s->childwrite_latency.min);
534 printm("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
535 printm("%.6f:", s->childwrite_latency.max);
538 printf("CTDB version %u\n", CTDB_PROTOCOL);
539 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
540 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
542 for (i=0;i<ARRAY_SIZE(fields);i++) {
543 if (strchr(fields[i].name, '.')) {
544 preflen = strcspn(fields[i].name, ".")+1;
545 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
546 prefix = fields[i].name;
547 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
552 printf(" %*s%-22s%*s%10u\n",
554 fields[i].name+preflen,
556 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
558 printf(" hop_count_buckets:");
559 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
560 printf(" %d", s->hop_count_bucket[i]);
563 printf(" lock_buckets:");
564 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
565 printf(" %d", s->locks.buckets[i]);
568 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);
570 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);
572 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);
574 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);
575 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);
578 talloc_free(tmp_ctx);
582 display remote ctdb statistics combined from all nodes
584 static int control_statistics_all(struct ctdb_context *ctdb)
587 struct ctdb_statistics statistics;
591 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
592 CTDB_NO_MEMORY(ctdb, nodes);
594 ZERO_STRUCT(statistics);
596 for (i=0;i<num_nodes;i++) {
597 struct ctdb_statistics s1;
599 uint32_t *v1 = (uint32_t *)&s1;
600 uint32_t *v2 = (uint32_t *)&statistics;
602 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
603 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
605 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
608 for (j=0;j<num_ints;j++) {
611 statistics.max_hop_count =
612 MAX(statistics.max_hop_count, s1.max_hop_count);
613 statistics.call_latency.max =
614 MAX(statistics.call_latency.max, s1.call_latency.max);
617 printf("Gathered statistics for %u nodes\n", num_nodes);
618 show_statistics(&statistics, 1);
623 display remote ctdb statistics
625 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
628 struct ctdb_statistics statistics;
630 if (options.pnn == CTDB_BROADCAST_ALL) {
631 return control_statistics_all(ctdb);
634 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
636 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
639 show_statistics(&statistics, 1);
645 reset remote ctdb statistics
647 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
651 ret = ctdb_statistics_reset(ctdb, options.pnn);
653 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
661 display remote ctdb rolling statistics
663 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
666 struct ctdb_statistics_wire *stats;
667 int i, num_records = -1;
669 assert_single_node_only();
672 num_records = atoi(argv[0]) - 1;
675 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
677 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
680 for (i=0;i<stats->num;i++) {
681 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
684 show_statistics(&stats->stats[i], i==0);
685 if (i == num_records) {
694 display remote ctdb db statistics
696 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
698 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
699 struct ctdb_db_statistics *dbstat;
709 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
713 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
715 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
716 talloc_free(tmp_ctx);
720 printf("DB Statistics: %s\n", argv[0]);
721 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
722 dbstat->db_ro_delegations);
723 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
724 dbstat->db_ro_delegations);
725 printf(" %s\n", "locks");
726 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
727 dbstat->locks.num_calls);
728 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
729 dbstat->locks.num_failed);
730 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
731 dbstat->locks.num_current);
732 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
733 dbstat->locks.num_pending);
734 printf(" %s", "hop_count_buckets:");
735 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
736 printf(" %d", dbstat->hop_count_bucket[i]);
739 printf(" %s", "lock_buckets:");
740 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
741 printf(" %d", dbstat->locks.buckets[i]);
744 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
745 "locks_latency MIN/AVG/MAX",
746 dbstat->locks.latency.min,
747 (dbstat->locks.latency.num ?
748 dbstat->locks.latency.total /dbstat->locks.latency.num :
750 dbstat->locks.latency.max,
751 dbstat->locks.latency.num);
752 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
753 "vacuum_latency MIN/AVG/MAX",
754 dbstat->vacuum.latency.min,
755 (dbstat->vacuum.latency.num ?
756 dbstat->vacuum.latency.total /dbstat->vacuum.latency.num :
758 dbstat->vacuum.latency.max,
759 dbstat->vacuum.latency.num);
761 for (i=0; i<dbstat->num_hot_keys; i++) {
762 if (dbstat->hot_keys[i].count > 0) {
766 dbstat->num_hot_keys = num_hot_keys;
768 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
769 for (i = 0; i < dbstat->num_hot_keys; i++) {
771 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
772 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
773 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
778 talloc_free(tmp_ctx);
783 display uptime of remote node
785 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
788 struct ctdb_uptime *uptime = NULL;
789 int tmp, days, hours, minutes, seconds;
791 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
793 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
797 if (options.machinereadable){
798 printm(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
799 printm(":%u:%u:%u:%lf\n",
800 (unsigned int)uptime->current_time.tv_sec,
801 (unsigned int)uptime->ctdbd_start_time.tv_sec,
802 (unsigned int)uptime->last_recovery_finished.tv_sec,
803 timeval_delta(&uptime->last_recovery_finished,
804 &uptime->last_recovery_started)
809 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
811 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
819 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
821 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
829 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
831 printf("Duration of last recovery/failover: %lf seconds\n",
832 timeval_delta(&uptime->last_recovery_finished,
833 &uptime->last_recovery_started));
839 show the PNN of the current node
841 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
845 mypnn = getpnn(ctdb);
847 printf("PNN:%d\n", mypnn);
853 struct pnn_node *next, *prev;
858 static struct pnn_node *read_pnn_node_file(TALLOC_CTX *mem_ctx,
864 struct pnn_node *pnn_nodes = NULL;
865 struct pnn_node *pnn_node;
867 lines = file_lines_load(file, &nlines, 0, mem_ctx);
871 for (i=0, pnn=0; i<nlines; i++) {
875 /* strip leading spaces */
876 while((*node == ' ') || (*node == '\t')) {
883 if (strcmp(node, "") == 0) {
886 pnn_node = talloc(mem_ctx, struct pnn_node);
887 pnn_node->pnn = pnn++;
889 if (!parse_ip(node, NULL, 0, &pnn_node->addr)) {
891 ("Invalid IP address '%s' in file %s\n",
893 /* Caller will free mem_ctx */
897 DLIST_ADD_END(pnn_nodes, pnn_node, NULL);
903 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
905 const char *nodes_list;
907 /* read the nodes file */
908 nodes_list = getenv("CTDB_NODES");
909 if (nodes_list == NULL) {
910 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
911 getenv("CTDB_BASE"));
912 if (nodes_list == NULL) {
913 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
918 return read_pnn_node_file(mem_ctx, nodes_list);
922 show the PNN of the current node
923 discover the pnn by loading the nodes file and try to bind to all
924 addresses one at a time until the ip address is found.
926 static int find_node_xpnn(void)
928 TALLOC_CTX *mem_ctx = talloc_new(NULL);
929 struct pnn_node *pnn_nodes;
930 struct pnn_node *pnn_node;
933 pnn_nodes = read_nodes_file(mem_ctx);
934 if (pnn_nodes == NULL) {
935 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
936 talloc_free(mem_ctx);
940 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
941 if (ctdb_sys_have_ip(&pnn_node->addr)) {
943 talloc_free(mem_ctx);
948 printf("Failed to detect which PNN this node is\n");
949 talloc_free(mem_ctx);
953 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
957 assert_single_node_only();
959 pnn = find_node_xpnn();
964 printf("PNN:%d\n", pnn);
968 /* Helpers for ctdb status
970 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
972 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
976 if (node->flags == 0) {
977 struct ctdb_control_get_ifaces *ifaces;
979 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
980 tmp_ctx, &ifaces) == 0) {
981 for (j=0; j < ifaces->num; j++) {
982 if (ifaces->ifaces[j].link_state != 0) {
990 talloc_free(tmp_ctx);
995 static void control_status_header_machine(void)
997 printm(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
998 ":Inactive:PartiallyOnline:ThisNode:\n");
1001 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
1002 struct ctdb_node_and_flags *node)
1004 printm(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
1005 ctdb_addr_to_str(&node->addr),
1006 !!(node->flags&NODE_FLAGS_DISCONNECTED),
1007 !!(node->flags&NODE_FLAGS_BANNED),
1008 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
1009 !!(node->flags&NODE_FLAGS_UNHEALTHY),
1010 !!(node->flags&NODE_FLAGS_STOPPED),
1011 !!(node->flags&NODE_FLAGS_INACTIVE),
1012 is_partially_online(ctdb, node) ? 1 : 0,
1013 (node->pnn == mypnn)?'Y':'N');
1018 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
1019 struct ctdb_node_and_flags *node)
1021 printf("pnn:%d %-16s %s%s\n", node->pnn,
1022 ctdb_addr_to_str(&node->addr),
1023 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
1024 node->pnn == mypnn?" (THIS NODE)":"");
1030 display remote ctdb status
1032 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
1034 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1036 struct ctdb_vnn_map *vnnmap=NULL;
1037 struct ctdb_node_map *nodemap=NULL;
1038 uint32_t recmode, recmaster, mypnn;
1039 int num_deleted_nodes = 0;
1042 mypnn = getpnn(ctdb);
1044 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1046 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1047 talloc_free(tmp_ctx);
1051 if (options.machinereadable) {
1052 control_status_header_machine();
1053 for (i=0;i<nodemap->num;i++) {
1054 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1057 (void) control_status_1_machine(ctdb, mypnn,
1058 &nodemap->nodes[i]);
1060 talloc_free(tmp_ctx);
1064 for (i=0; i<nodemap->num; i++) {
1065 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1066 num_deleted_nodes++;
1069 if (num_deleted_nodes == 0) {
1070 printf("Number of nodes:%d\n", nodemap->num);
1072 printf("Number of nodes:%d (including %d deleted nodes)\n",
1073 nodemap->num, num_deleted_nodes);
1075 for(i=0;i<nodemap->num;i++){
1076 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1079 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1082 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1084 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1085 talloc_free(tmp_ctx);
1088 if (vnnmap->generation == INVALID_GENERATION) {
1089 printf("Generation:INVALID\n");
1091 printf("Generation:%d\n",vnnmap->generation);
1093 printf("Size:%d\n",vnnmap->size);
1094 for(i=0;i<vnnmap->size;i++){
1095 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1098 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1100 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1101 talloc_free(tmp_ctx);
1104 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1106 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1108 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1109 talloc_free(tmp_ctx);
1112 printf("Recovery master:%d\n",recmaster);
1114 talloc_free(tmp_ctx);
1118 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1120 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1122 struct ctdb_node_map *nodemap=NULL;
1124 uint32_t pnn_mode, mypnn;
1130 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1131 options.pnn, true, &nodes, &pnn_mode)) {
1135 if (options.machinereadable) {
1136 control_status_header_machine();
1137 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1138 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1141 mypnn = getpnn(ctdb);
1143 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1145 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1146 talloc_free(tmp_ctx);
1152 for (i = 0; i < talloc_array_length(nodes); i++) {
1153 if (options.machinereadable) {
1154 ret |= control_status_1_machine(ctdb, mypnn,
1155 &nodemap->nodes[nodes[i]]);
1157 ret |= control_status_1_human(ctdb, mypnn,
1158 &nodemap->nodes[nodes[i]]);
1162 talloc_free(tmp_ctx);
1166 static struct pnn_node *read_natgw_nodes_file(struct ctdb_context *ctdb,
1167 TALLOC_CTX *mem_ctx)
1169 const char *natgw_list;
1170 struct pnn_node *natgw_nodes = NULL;
1172 natgw_list = getenv("CTDB_NATGW_NODES");
1173 if (natgw_list == NULL) {
1174 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1175 getenv("CTDB_BASE"));
1176 if (natgw_list == NULL) {
1177 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1181 /* The PNNs will be junk but they're not used */
1182 natgw_nodes = read_pnn_node_file(mem_ctx, natgw_list);
1183 if (natgw_nodes == NULL) {
1185 ("Failed to load natgw node list '%s'\n", natgw_list));
1191 /* talloc off the existing nodemap... */
1192 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1194 return talloc_zero_size(nodemap,
1195 offsetof(struct ctdb_node_map, nodes) +
1196 nodemap->num * sizeof(struct ctdb_node_and_flags));
1199 static struct ctdb_node_map *
1200 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1201 struct ctdb_node_map *nodemap,
1202 struct pnn_node *nodes)
1206 struct ctdb_node_map *ret;
1208 ret = talloc_nodemap(nodemap);
1209 CTDB_NO_MEMORY_NULL(ctdb, ret);
1213 for (i = 0; i < nodemap->num; i++) {
1214 for(n = nodes; n != NULL ; n = n->next) {
1215 if (ctdb_same_ip(&n->addr,
1216 &nodemap->nodes[i].addr)) {
1224 ret->nodes[ret->num] = nodemap->nodes[i];
1231 static struct ctdb_node_map *
1232 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1233 struct ctdb_node_map *nodemap,
1234 uint32_t required_capabilities,
1238 uint32_t capabilities;
1239 struct ctdb_node_map *ret;
1241 ret = talloc_nodemap(nodemap);
1242 CTDB_NO_MEMORY_NULL(ctdb, ret);
1246 for (i = 0; i < nodemap->num; i++) {
1249 /* Disconnected nodes have no capabilities! */
1250 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1254 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1255 nodemap->nodes[i].pnn,
1258 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1259 nodemap->nodes[i].pnn));
1263 if (!(capabilities & required_capabilities)) {
1267 ret->nodes[ret->num] = nodemap->nodes[i];
1277 static struct ctdb_node_map *
1278 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1279 struct ctdb_node_map *nodemap,
1280 uint32_t flags_mask)
1283 struct ctdb_node_map *ret;
1285 ret = talloc_nodemap(nodemap);
1286 CTDB_NO_MEMORY_NULL(ctdb, ret);
1290 for (i = 0; i < nodemap->num; i++) {
1291 if (nodemap->nodes[i].flags & flags_mask) {
1295 ret->nodes[ret->num] = nodemap->nodes[i];
1303 display the list of nodes belonging to this natgw configuration
1305 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1307 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1309 struct pnn_node *natgw_nodes = NULL;
1310 struct ctdb_node_map *orig_nodemap=NULL;
1311 struct ctdb_node_map *nodemap;
1312 uint32_t mypnn, pnn;
1315 /* When we have some nodes that could be the NATGW, make a
1316 * series of attempts to find the first node that doesn't have
1317 * certain status flags set.
1319 uint32_t exclude_flags[] = {
1320 /* Look for a nice healthy node */
1321 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1322 /* If not found, an UNHEALTHY/BANNED node will do */
1323 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1324 /* If not found, a STOPPED node will do */
1325 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1329 /* read the natgw nodes file into a linked list */
1330 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1331 if (natgw_nodes == NULL) {
1336 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1337 tmp_ctx, &orig_nodemap);
1339 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1340 talloc_free(tmp_ctx);
1344 /* Get a nodemap that includes only the nodes in the NATGW
1346 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1347 if (nodemap == NULL) {
1352 ret = 2; /* matches ENOENT */
1355 /* For each flag mask... */
1356 for (i = 0; exclude_flags[i] != 0; i++) {
1357 /* ... get a nodemap that excludes nodes with with
1358 * masked flags... */
1359 struct ctdb_node_map *t =
1360 filter_nodemap_by_flags(ctdb, nodemap,
1368 /* ... and find the first node with the NATGW
1370 struct ctdb_node_map *n;
1371 n = filter_nodemap_by_capabilities(ctdb, t,
1381 pnn = n->nodes[0].pnn;
1382 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1389 if (options.machinereadable) {
1390 printm(":Node:IP:\n");
1391 printm(":%d:%s:\n", pnn, ip);
1393 printf("%d %s\n", pnn, ip);
1396 /* print the pruned list of nodes belonging to this natgw list */
1397 mypnn = getpnn(ctdb);
1398 if (options.machinereadable) {
1399 control_status_header_machine();
1401 printf("Number of nodes:%d\n", nodemap->num);
1403 for(i=0;i<nodemap->num;i++){
1404 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1407 if (options.machinereadable) {
1408 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1410 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1415 talloc_free(tmp_ctx);
1420 display the status of the scripts for monitoring (or other events)
1422 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1423 enum ctdb_eventscript_call type)
1425 struct ctdb_scripts_wire *script_status;
1428 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1430 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1434 if (script_status == NULL) {
1435 if (!options.machinereadable) {
1436 printf("%s cycle never run\n",
1437 ctdb_eventscript_call_names[type]);
1442 if (!options.machinereadable) {
1444 for (i=0; i<script_status->num_scripts; i++) {
1445 if (script_status->scripts[i].status != -ENOEXEC) {
1449 printf("%d scripts were executed last %s cycle\n",
1451 ctdb_eventscript_call_names[type]);
1453 for (i=0; i<script_status->num_scripts; i++) {
1454 const char *status = NULL;
1456 switch (script_status->scripts[i].status) {
1458 status = "TIMEDOUT";
1461 status = "DISABLED";
1467 if (script_status->scripts[i].status > 0)
1471 if (options.machinereadable) {
1472 printm(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1473 ctdb_eventscript_call_names[type],
1474 script_status->scripts[i].name,
1475 script_status->scripts[i].status,
1477 (long)script_status->scripts[i].start.tv_sec,
1478 (long)script_status->scripts[i].start.tv_usec,
1479 (long)script_status->scripts[i].finished.tv_sec,
1480 (long)script_status->scripts[i].finished.tv_usec,
1481 script_status->scripts[i].output);
1485 printf("%-20s Status:%s ",
1486 script_status->scripts[i].name, status);
1488 /* Some other error, eg from stat. */
1489 printf("%-20s Status:CANNOT RUN (%s)",
1490 script_status->scripts[i].name,
1491 strerror(-script_status->scripts[i].status));
1493 if (script_status->scripts[i].status >= 0) {
1494 printf("Duration:%.3lf ",
1495 timeval_delta(&script_status->scripts[i].finished,
1496 &script_status->scripts[i].start));
1498 if (script_status->scripts[i].status != -ENOEXEC) {
1500 ctime(&script_status->scripts[i].start.tv_sec));
1501 if (script_status->scripts[i].status != 0) {
1502 printf(" OUTPUT:%s\n",
1503 script_status->scripts[i].output);
1513 static int control_scriptstatus(struct ctdb_context *ctdb,
1514 int argc, const char **argv)
1517 enum ctdb_eventscript_call type, min, max;
1521 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1526 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1530 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1531 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1537 if (type == CTDB_EVENT_MAX) {
1538 if (strcmp(arg, "all") == 0) {
1540 max = CTDB_EVENT_MAX;
1542 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1547 if (options.machinereadable) {
1548 printm(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1551 for (type = min; type < max; type++) {
1552 ret = control_one_scriptstatus(ctdb, type);
1562 enable an eventscript
1564 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1572 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1574 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1582 disable an eventscript
1584 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1592 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1594 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1602 display the pnn of the recovery master
1604 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1609 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1611 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1614 printf("%d\n",recmaster);
1620 add a tickle to a public address
1622 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1624 struct ctdb_tcp_connection t;
1628 assert_single_node_only();
1634 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1635 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1638 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1639 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1643 data.dptr = (uint8_t *)&t;
1644 data.dsize = sizeof(t);
1646 /* tell all nodes about this tcp connection */
1647 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1648 0, data, ctdb, NULL, NULL, NULL, NULL);
1650 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1659 delete a tickle from a node
1661 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1663 struct ctdb_tcp_connection t;
1667 assert_single_node_only();
1673 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1674 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1677 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1678 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1682 data.dptr = (uint8_t *)&t;
1683 data.dsize = sizeof(t);
1685 /* tell all nodes about this tcp connection */
1686 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1687 0, data, ctdb, NULL, NULL, NULL, NULL);
1689 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1698 get a list of all tickles for this pnn
1700 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1702 struct ctdb_control_tcp_tickle_list *list;
1703 ctdb_sock_addr addr;
1707 assert_single_node_only();
1714 port = atoi(argv[1]);
1717 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1718 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1722 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1724 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1728 if (options.machinereadable){
1729 printm(":source ip:port:destination ip:port:\n");
1730 for (i=0;i<list->tickles.num;i++) {
1731 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1734 printm(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1735 printm(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1738 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1739 printf("Num tickles:%u\n", list->tickles.num);
1740 for (i=0;i<list->tickles.num;i++) {
1741 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1744 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1745 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));
1755 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1757 struct ctdb_all_public_ips *ips;
1758 struct ctdb_public_ip ip;
1761 uint32_t disable_time;
1763 struct ctdb_node_map *nodemap=NULL;
1764 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1767 data.dptr = (uint8_t*)&disable_time;
1768 data.dsize = sizeof(disable_time);
1769 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1771 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1777 /* read the public ip list from the node */
1778 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1780 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1781 talloc_free(tmp_ctx);
1785 for (i=0;i<ips->num;i++) {
1786 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1791 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1792 pnn, ctdb_addr_to_str(addr)));
1793 talloc_free(tmp_ctx);
1800 data.dptr = (uint8_t *)&ip;
1801 data.dsize = sizeof(ip);
1803 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1805 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1806 talloc_free(tmp_ctx);
1810 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1811 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1818 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1819 talloc_free(tmp_ctx);
1823 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1825 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1826 talloc_free(tmp_ctx);
1830 /* update the recovery daemon so it now knows to expect the new
1831 node assignment for this ip.
1833 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1835 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1839 talloc_free(tmp_ctx);
1845 * scans all other nodes and returns a pnn for another node that can host this
1849 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1851 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1852 struct ctdb_all_public_ips *ips;
1853 struct ctdb_node_map *nodemap=NULL;
1857 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1859 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1860 talloc_free(tmp_ctx);
1864 for(i=0;i<nodemap->num;i++){
1865 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1868 if (nodemap->nodes[i].pnn == options.pnn) {
1872 /* read the public ip list from this node */
1873 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1875 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1879 for (j=0;j<ips->num;j++) {
1880 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1881 pnn = nodemap->nodes[i].pnn;
1882 talloc_free(tmp_ctx);
1889 talloc_free(tmp_ctx);
1893 /* If pnn is -1 then try to find a node to move IP to... */
1894 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1896 bool pnn_specified = (pnn == -1 ? false : true);
1899 while (retries < 5) {
1900 if (!pnn_specified) {
1901 pnn = find_other_host_for_public_ip(ctdb, addr);
1906 ("Trying to move public IP to node %u\n", pnn));
1909 if (move_ip(ctdb, addr, pnn) == 0) {
1922 move/failover an ip address to a specific node
1924 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1927 ctdb_sock_addr addr;
1929 assert_single_node_only();
1936 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1937 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1942 if (sscanf(argv[1], "%u", &pnn) != 1) {
1943 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1947 if (!try_moveip(ctdb, &addr, pnn)) {
1948 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1955 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1959 data.dptr = (uint8_t *)&pnn;
1960 data.dsize = sizeof(uint32_t);
1961 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1963 ("Failed to send message to force node %u to be a rebalancing target\n",
1973 rebalance a node by setting it to allow failback and triggering a
1976 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1978 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1983 assert_single_node_only();
1989 /* Determine the nodes where IPs need to be reloaded */
1990 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1991 options.pnn, true, &nodes, &pnn_mode)) {
1996 for (i = 0; i < talloc_array_length(nodes); i++) {
1997 if (!rebalance_node(ctdb, nodes[i])) {
2003 talloc_free(tmp_ctx);
2007 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
2009 struct ctdb_public_ip ip;
2012 uint32_t disable_time;
2014 struct ctdb_node_map *nodemap=NULL;
2015 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2018 data.dptr = (uint8_t*)&disable_time;
2019 data.dsize = sizeof(disable_time);
2020 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
2022 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
2029 data.dptr = (uint8_t *)&ip;
2030 data.dsize = sizeof(ip);
2032 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
2034 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2035 talloc_free(tmp_ctx);
2039 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2040 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
2047 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
2048 talloc_free(tmp_ctx);
2052 talloc_free(tmp_ctx);
2057 release an ip form all nodes and have it re-assigned by recd
2059 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2061 ctdb_sock_addr addr;
2063 assert_single_node_only();
2070 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2071 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2075 if (rebalance_ip(ctdb, &addr) != 0) {
2076 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2083 static int getips_store_callback(void *param, void *data)
2085 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2086 struct ctdb_all_public_ips *ips = param;
2090 ips->ips[i].pnn = node_ip->pnn;
2091 ips->ips[i].addr = node_ip->addr;
2095 static int getips_count_callback(void *param, void *data)
2097 uint32_t *count = param;
2104 static uint32_t *ip_key(ctdb_sock_addr *ip)
2106 static uint32_t key[IP_KEYLEN];
2108 bzero(key, sizeof(key));
2110 switch (ip->sa.sa_family) {
2112 key[0] = ip->ip.sin_addr.s_addr;
2115 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2123 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2130 static void *add_ip_callback(void *parm, void *data)
2136 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2138 struct ctdb_all_public_ips *tmp_ips;
2139 struct ctdb_node_map *nodemap=NULL;
2140 trbt_tree_t *ip_tree;
2144 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2146 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2150 ip_tree = trbt_create(tmp_ctx, 0);
2152 for(i=0;i<nodemap->num;i++){
2153 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2156 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2160 /* read the public ip list from this node */
2161 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2163 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2167 for (j=0; j<tmp_ips->num;j++) {
2168 struct ctdb_public_ip *node_ip;
2170 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2171 node_ip->pnn = tmp_ips->ips[j].pnn;
2172 node_ip->addr = tmp_ips->ips[j].addr;
2174 trbt_insertarray32_callback(ip_tree,
2175 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2179 talloc_free(tmp_ips);
2184 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2186 len = offsetof(struct ctdb_all_public_ips, ips) +
2187 count*sizeof(struct ctdb_public_ip);
2188 tmp_ips = talloc_zero_size(tmp_ctx, len);
2189 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2197 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2199 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2201 event_add_timed(ctdb->ev, ctdb,
2202 timeval_current_ofs(1, 0),
2203 ctdb_every_second, ctdb);
2206 struct srvid_reply_handler_data {
2210 const char *srvid_str;
2213 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2218 struct srvid_reply_handler_data *d =
2219 (struct srvid_reply_handler_data *)private_data;
2223 if (data.dsize != sizeof(ret)) {
2224 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2228 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2229 ret = *(int32_t *)data.dptr;
2232 ("%s failed with result %d\n", d->srvid_str, ret));
2236 if (!d->wait_for_all) {
2241 /* Wait for all replies */
2243 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2244 if (d->nodes[i] == ret) {
2246 ("%s reply received from node %u\n",
2247 d->srvid_str, ret));
2250 if (d->nodes[i] != -1) {
2251 /* Found a node that hasn't yet replied */
2257 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2258 * or replies from all connected nodes. arg is the data argument to
2259 * pass in the srvid_request structure - pass 0 if this isn't needed.
2261 static int srvid_broadcast(struct ctdb_context *ctdb,
2262 uint64_t srvid, uint32_t *arg,
2263 const char *srvid_str, bool wait_for_all)
2268 uint64_t reply_srvid;
2269 struct srvid_request request;
2270 struct srvid_request_data request_data;
2271 struct srvid_reply_handler_data reply_data;
2274 ZERO_STRUCT(request);
2276 /* Time ticks to enable timeouts to be processed */
2277 event_add_timed(ctdb->ev, ctdb,
2278 timeval_current_ofs(1, 0),
2279 ctdb_every_second, ctdb);
2281 pnn = ctdb_get_pnn(ctdb);
2282 reply_srvid = getpid();
2286 request.srvid = reply_srvid;
2288 data.dptr = (uint8_t *)&request;
2289 data.dsize = sizeof(request);
2291 request_data.pnn = pnn;
2292 request_data.srvid = reply_srvid;
2293 request_data.data = *arg;
2295 data.dptr = (uint8_t *)&request_data;
2296 data.dsize = sizeof(request_data);
2299 /* Register message port for reply from recovery master */
2300 ctdb_client_set_message_handler(ctdb, reply_srvid,
2301 srvid_broadcast_reply_handler,
2304 reply_data.wait_for_all = wait_for_all;
2305 reply_data.nodes = NULL;
2306 reply_data.srvid_str = srvid_str;
2309 reply_data.done = false;
2312 struct ctdb_node_map *nodemap;
2314 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2315 CTDB_CURRENT_NODE, ctdb, &nodemap);
2318 ("Unable to get nodemap from current node, try again\n"));
2323 if (reply_data.nodes != NULL) {
2324 talloc_free(reply_data.nodes);
2326 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2329 talloc_free(nodemap);
2332 /* Send to all connected nodes. Only recmaster replies */
2333 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2336 /* This can only happen if the socket is closed and
2337 * there's no way to recover from that, so don't try
2341 ("Failed to send %s request to connected nodes\n",
2346 tv = timeval_current();
2347 /* This loop terminates the reply is received */
2348 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2349 event_loop_once(ctdb->ev);
2352 if (!reply_data.done) {
2354 ("Still waiting for confirmation of %s\n", srvid_str));
2359 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2361 talloc_free(reply_data.nodes);
2366 static int ipreallocate(struct ctdb_context *ctdb)
2368 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2369 "IP reallocation", false);
2373 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2375 return ipreallocate(ctdb);
2379 add a public ip address to a node
2381 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2384 int len, retries = 0;
2386 ctdb_sock_addr addr;
2387 struct ctdb_control_ip_iface *pub;
2388 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2389 struct ctdb_all_public_ips *ips;
2393 talloc_free(tmp_ctx);
2397 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2398 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2399 talloc_free(tmp_ctx);
2403 /* read the public ip list from the node */
2404 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2406 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2407 talloc_free(tmp_ctx);
2410 for (i=0;i<ips->num;i++) {
2411 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2412 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2419 /* Dont timeout. This command waits for an ip reallocation
2420 which sometimes can take wuite a while if there has
2421 been a recent recovery
2425 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2426 pub = talloc_size(tmp_ctx, len);
2427 CTDB_NO_MEMORY(ctdb, pub);
2431 pub->len = strlen(argv[1])+1;
2432 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2435 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2437 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2441 } while (retries < 5 && ret != 0);
2443 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2444 talloc_free(tmp_ctx);
2448 if (rebalance_node(ctdb, options.pnn) != 0) {
2449 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2453 talloc_free(tmp_ctx);
2458 add a public ip address to a node
2460 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2462 ctdb_sock_addr addr;
2468 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2469 printf("Badly formed ip : %s\n", argv[0]);
2473 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
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_control_killtcp *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_control_killtcp,
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_control_killtcp);
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_control_killtcp 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)
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 (options.pnn == CTDB_BROADCAST_ALL) {
2969 /* read the list of public ips from all nodes */
2970 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2972 /* read the public ip list from this node */
2973 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2976 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2977 talloc_free(tmp_ctx);
2981 if (options.machinereadable){
2982 printm(":Public IP:Node:");
2983 if (options.verbose){
2984 printm("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2988 if (options.pnn == CTDB_BROADCAST_ALL) {
2989 printf("Public IPs on ALL nodes\n");
2991 printf("Public IPs on node %u\n", options.pnn);
2995 for (i=1;i<=ips->num;i++) {
2996 struct ctdb_control_public_ip_info *info = NULL;
2998 char *aciface = NULL;
2999 char *avifaces = NULL;
3000 char *cifaces = NULL;
3002 if (options.pnn == CTDB_BROADCAST_ALL) {
3003 pnn = ips->ips[ips->num-i].pnn;
3009 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
3010 &ips->ips[ips->num-i].addr, &info);
3017 for (j=0; j < info->num; j++) {
3018 if (cifaces == NULL) {
3019 cifaces = talloc_strdup(info,
3020 info->ifaces[j].name);
3022 cifaces = talloc_asprintf_append(cifaces,
3024 info->ifaces[j].name);
3027 if (info->active_idx == j) {
3028 aciface = info->ifaces[j].name;
3031 if (info->ifaces[j].link_state == 0) {
3035 if (avifaces == NULL) {
3036 avifaces = talloc_strdup(info, info->ifaces[j].name);
3038 avifaces = talloc_asprintf_append(avifaces,
3040 info->ifaces[j].name);
3045 if (options.machinereadable){
3047 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3048 ips->ips[ips->num-i].pnn);
3049 if (options.verbose){
3052 avifaces?avifaces:"",
3053 cifaces?cifaces:"");
3057 if (options.verbose) {
3058 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3059 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3060 ips->ips[ips->num-i].pnn,
3062 avifaces?avifaces:"",
3063 cifaces?cifaces:"");
3066 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3067 ips->ips[ips->num-i].pnn);
3073 talloc_free(tmp_ctx);
3080 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3083 ctdb_sock_addr addr;
3084 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3085 struct ctdb_control_public_ip_info *info;
3088 talloc_free(tmp_ctx);
3092 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3093 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3097 /* read the public ip info from this node */
3098 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3099 tmp_ctx, &addr, &info);
3101 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3102 argv[0], options.pnn));
3103 talloc_free(tmp_ctx);
3107 printf("Public IP[%s] info on node %u\n",
3108 ctdb_addr_to_str(&info->ip.addr),
3111 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3112 ctdb_addr_to_str(&info->ip.addr),
3113 info->ip.pnn, info->num);
3115 for (i=0; i<info->num; i++) {
3116 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3118 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3119 i+1, info->ifaces[i].name,
3120 info->ifaces[i].link_state?"up":"down",
3121 (unsigned int)info->ifaces[i].references,
3122 (i==info->active_idx)?" (active)":"");
3125 talloc_free(tmp_ctx);
3130 display interfaces status
3132 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3134 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3136 struct ctdb_control_get_ifaces *ifaces;
3139 /* read the public ip list from this node */
3140 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3142 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3144 talloc_free(tmp_ctx);
3148 if (options.machinereadable){
3149 printm(":Name:LinkStatus:References:\n");
3151 printf("Interfaces on node %u\n", options.pnn);
3154 for (i=0; i<ifaces->num; i++) {
3155 if (options.machinereadable){
3156 printm(":%s:%s:%u\n",
3157 ifaces->ifaces[i].name,
3158 ifaces->ifaces[i].link_state?"1":"0",
3159 (unsigned int)ifaces->ifaces[i].references);
3161 printf("name:%s link:%s references:%u\n",
3162 ifaces->ifaces[i].name,
3163 ifaces->ifaces[i].link_state?"up":"down",
3164 (unsigned int)ifaces->ifaces[i].references);
3168 talloc_free(tmp_ctx);
3174 set link status of an interface
3176 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3179 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3180 struct ctdb_control_iface_info info;
3188 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3189 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3191 talloc_free(tmp_ctx);
3194 strcpy(info.name, argv[0]);
3196 if (strcmp(argv[1], "up") == 0) {
3197 info.link_state = 1;
3198 } else if (strcmp(argv[1], "down") == 0) {
3199 info.link_state = 0;
3201 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3203 talloc_free(tmp_ctx);
3207 /* read the public ip list from this node */
3208 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3211 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3212 argv[0], options.pnn));
3213 talloc_free(tmp_ctx);
3217 talloc_free(tmp_ctx);
3222 display pid of a ctdb daemon
3224 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3229 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3231 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3234 printf("Pid:%d\n", pid);
3239 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3241 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3243 update_flags_handler_t handler,
3248 struct ctdb_node_map *nodemap = NULL;
3252 /* Check if the node is already in the desired state */
3253 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3255 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3258 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3259 if (set_flag == flag_is_set) {
3260 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3261 (set_flag ? "already" : "not"), desc));
3266 if (!handler(ctdb, data)) {
3267 DEBUG(DEBUG_WARNING,
3268 ("Failed to send control to set state %s on node %u, try again\n",
3269 desc, options.pnn));
3274 /* Read the nodemap and verify the change took effect.
3275 * Even if the above control/hanlder timed out then it
3276 * could still have worked!
3278 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3281 DEBUG(DEBUG_WARNING,
3282 ("Unable to get nodemap from local node, try again\n"));
3284 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3285 } while (nodemap == NULL || (set_flag != flag_is_set));
3287 return ipreallocate(ctdb);
3290 /* Administratively disable a node */
3291 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3295 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3296 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3300 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3302 return update_flags_and_ipreallocate(ctdb, NULL,
3303 update_flags_disabled,
3304 NODE_FLAGS_PERMANENTLY_DISABLED,
3306 true /* set_flag*/);
3309 /* Administratively re-enable a node */
3310 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3314 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3315 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3319 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3321 return update_flags_and_ipreallocate(ctdb, NULL,
3322 update_flags_not_disabled,
3323 NODE_FLAGS_PERMANENTLY_DISABLED,
3325 false /* set_flag*/);
3329 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3333 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3338 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3340 return update_flags_and_ipreallocate(ctdb, NULL,
3341 update_flags_stopped,
3344 true /* set_flag*/);
3347 /* Continue a stopped node */
3348 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3352 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3357 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3359 return update_flags_and_ipreallocate(ctdb, NULL,
3360 update_flags_not_stopped,
3363 false /* set_flag */);
3366 static uint32_t get_generation(struct ctdb_context *ctdb)
3368 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3369 struct ctdb_vnn_map *vnnmap=NULL;
3371 uint32_t generation;
3373 /* wait until the recmaster is not in recovery mode */
3375 uint32_t recmode, recmaster;
3377 if (vnnmap != NULL) {
3378 talloc_free(vnnmap);
3382 /* get the recmaster */
3383 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3385 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3386 talloc_free(tmp_ctx);
3390 /* get recovery mode */
3391 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3393 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3394 talloc_free(tmp_ctx);
3398 /* get the current generation number */
3399 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3401 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3402 talloc_free(tmp_ctx);
3406 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3407 generation = vnnmap->generation;
3408 talloc_free(tmp_ctx);
3416 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3418 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3421 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3426 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3428 struct ctdb_ban_time bantime;
3434 bantime.pnn = options.pnn;
3435 bantime.time = strtoul(argv[0], NULL, 0);
3437 if (bantime.time == 0) {
3438 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3442 return update_flags_and_ipreallocate(ctdb, &bantime,
3443 update_state_banned,
3446 true /* set_flag*/);
3451 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3453 struct ctdb_ban_time bantime;
3455 bantime.pnn = options.pnn;
3458 return update_flags_and_ipreallocate(ctdb, &bantime,
3459 update_state_banned,
3462 false /* set_flag*/);
3466 show ban information for a node
3468 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3471 struct ctdb_node_map *nodemap=NULL;
3472 struct ctdb_ban_time *bantime;
3474 /* verify the node exists */
3475 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3477 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3481 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3483 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3487 if (bantime->time == 0) {
3488 printf("Node %u is not banned\n", bantime->pnn);
3490 printf("Node %u is banned, %d seconds remaining\n",
3491 bantime->pnn, bantime->time);
3500 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3504 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3506 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3516 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3519 uint32_t generation, next_generation;
3521 /* record the current generation number */
3522 generation = get_generation(ctdb);
3524 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3526 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3530 /* wait until we are in a new generation */
3532 next_generation = get_generation(ctdb);
3533 if (next_generation != generation) {
3544 display monitoring mode of a remote node
3546 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3551 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3553 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3556 if (!options.machinereadable){
3557 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3560 printm(":%d:\n",monmode);
3567 display capabilities of a remote node
3569 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3571 uint32_t capabilities;
3574 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3576 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3580 if (!options.machinereadable){
3581 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3582 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3583 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3584 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3586 printm(":RECMASTER:LMASTER:LVS:NATGW:\n");
3587 printm(":%d:%d:%d:%d:\n",
3588 !!(capabilities&CTDB_CAP_RECMASTER),
3589 !!(capabilities&CTDB_CAP_LMASTER),
3590 !!(capabilities&CTDB_CAP_LVS),
3591 !!(capabilities&CTDB_CAP_NATGW));
3597 display lvs configuration
3600 static uint32_t lvs_exclude_flags[] = {
3601 /* Look for a nice healthy node */
3602 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3603 /* If not found, an UNHEALTHY node will do */
3604 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3608 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3610 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3611 struct ctdb_node_map *orig_nodemap=NULL;
3612 struct ctdb_node_map *nodemap;
3615 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3616 tmp_ctx, &orig_nodemap);
3618 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3619 talloc_free(tmp_ctx);
3623 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3624 CTDB_CAP_LVS, false);
3625 if (nodemap == NULL) {
3633 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3634 struct ctdb_node_map *t =
3635 filter_nodemap_by_flags(ctdb, nodemap,
3636 lvs_exclude_flags[i]);
3643 /* At least 1 node without excluded flags */
3645 for (j = 0; j < t->num; j++) {
3646 printf("%d:%s\n", t->nodes[j].pnn,
3647 ctdb_addr_to_str(&t->nodes[j].addr));
3654 talloc_free(tmp_ctx);
3659 display who is the lvs master
3661 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3663 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3664 struct ctdb_node_map *nodemap=NULL;
3667 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3670 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3671 talloc_free(tmp_ctx);
3675 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3676 struct ctdb_node_map *t =
3677 filter_nodemap_by_flags(ctdb, nodemap,
3678 lvs_exclude_flags[i]);
3685 struct ctdb_node_map *n;
3686 n = filter_nodemap_by_capabilities(ctdb,
3697 if (options.machinereadable) {
3698 printm("%d\n", n->nodes[0].pnn);
3700 printf("Node %d is LVS master\n", n->nodes[0].pnn);
3708 printf("There is no LVS master\n");
3711 talloc_free(tmp_ctx);
3716 disable monitoring on a node
3718 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3723 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3725 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3728 printf("Monitoring mode:%s\n","DISABLED");
3734 enable monitoring on a node
3736 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3741 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3743 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3746 printf("Monitoring mode:%s\n","ACTIVE");
3752 display remote list of keys/data for a db
3754 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3756 const char *db_name;
3757 struct ctdb_db_context *ctdb_db;
3759 struct ctdb_dump_db_context c;
3766 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3770 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3771 if (ctdb_db == NULL) {
3772 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3776 if (options.printlmaster) {
3777 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3778 ctdb, &ctdb->vnn_map);
3780 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3788 c.printemptyrecords = (bool)options.printemptyrecords;
3789 c.printdatasize = (bool)options.printdatasize;
3790 c.printlmaster = (bool)options.printlmaster;
3791 c.printhash = (bool)options.printhash;
3792 c.printrecordflags = (bool)options.printrecordflags;
3794 /* traverse and dump the cluster tdb */
3795 ret = ctdb_dump_db(ctdb_db, &c);
3797 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3798 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3799 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3803 talloc_free(ctdb_db);
3805 printf("Dumped %d records\n", ret);
3809 struct cattdb_data {
3810 struct ctdb_context *ctdb;
3814 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3816 struct cattdb_data *d = private_data;
3817 struct ctdb_dump_db_context c;
3823 c.printemptyrecords = (bool)options.printemptyrecords;
3824 c.printdatasize = (bool)options.printdatasize;
3825 c.printlmaster = false;
3826 c.printhash = (bool)options.printhash;
3827 c.printrecordflags = true;
3829 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3833 cat the local tdb database using same format as catdb
3835 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3837 const char *db_name;
3838 struct ctdb_db_context *ctdb_db;
3839 struct cattdb_data d;
3846 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3850 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3851 if (ctdb_db == NULL) {
3852 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3856 /* traverse the local tdb */
3859 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3860 printf("Failed to cattdb data\n");
3863 talloc_free(ctdb_db);
3865 printf("Dumped %d records\n", d.count);
3870 display the content of a database key
3872 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3874 const char *db_name;
3875 struct ctdb_db_context *ctdb_db;
3876 struct ctdb_record_handle *h;
3877 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3885 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3889 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3890 if (ctdb_db == NULL) {
3891 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3895 key.dptr = discard_const(argv[1]);
3896 key.dsize = strlen((char *)key.dptr);
3898 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3900 printf("Failed to fetch record '%s' on node %d\n",
3901 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3902 talloc_free(tmp_ctx);
3906 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3908 talloc_free(tmp_ctx);
3909 talloc_free(ctdb_db);
3914 display the content of a database key
3916 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3918 const char *db_name;
3919 struct ctdb_db_context *ctdb_db;
3920 struct ctdb_record_handle *h;
3921 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3929 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3933 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3934 if (ctdb_db == NULL) {
3935 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3939 key.dptr = discard_const(argv[1]);
3940 key.dsize = strlen((char *)key.dptr);
3942 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3944 printf("Failed to fetch record '%s' on node %d\n",
3945 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3946 talloc_free(tmp_ctx);
3950 data.dptr = discard_const(argv[2]);
3951 data.dsize = strlen((char *)data.dptr);
3953 if (ctdb_record_store(h, data) != 0) {
3954 printf("Failed to store record\n");
3958 talloc_free(tmp_ctx);
3959 talloc_free(ctdb_db);
3964 fetch a record from a persistent database
3966 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3968 const char *db_name;
3969 struct ctdb_db_context *ctdb_db;
3970 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3971 struct ctdb_transaction_handle *h;
3978 talloc_free(tmp_ctx);
3982 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3983 talloc_free(tmp_ctx);
3987 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3989 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3990 talloc_free(tmp_ctx);
3994 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3995 if (ctdb_db == NULL) {
3996 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3997 talloc_free(tmp_ctx);
4001 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4003 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4004 talloc_free(tmp_ctx);
4008 key.dptr = discard_const(argv[1]);
4009 key.dsize = strlen(argv[1]);
4010 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
4012 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
4013 talloc_free(tmp_ctx);
4017 if (data.dsize == 0 || data.dptr == NULL) {
4018 DEBUG(DEBUG_ERR,("Record is empty\n"));
4019 talloc_free(tmp_ctx);
4024 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4026 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
4027 talloc_free(tmp_ctx);
4030 sys_write(fd, data.dptr, data.dsize);
4033 sys_write(1, data.dptr, data.dsize);
4036 /* abort the transaction */
4040 talloc_free(tmp_ctx);
4045 fetch a record from a tdb-file
4047 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4049 const char *tdb_file;
4052 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4061 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4063 printf("Failed to open TDB file %s\n", tdb_file);
4067 if (!strncmp(argv[1], "0x", 2)) {
4068 key = hextodata(tmp_ctx, argv[1] + 2);
4069 if (key.dsize == 0) {
4070 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4074 key.dptr = discard_const(argv[1]);
4075 key.dsize = strlen(argv[1]);
4078 data = tdb_fetch(tdb, key);
4079 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4080 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4088 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4090 printf("Failed to open output file %s\n", argv[2]);
4093 if (options.verbose){
4094 sys_write(fd, data.dptr, data.dsize);
4096 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4100 if (options.verbose){
4101 sys_write(1, data.dptr, data.dsize);
4103 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4107 talloc_free(tmp_ctx);
4112 store a record and header to a tdb-file
4114 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4116 const char *tdb_file;
4118 TDB_DATA key, value, data;
4119 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4120 struct ctdb_ltdb_header header;
4128 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4130 printf("Failed to open TDB file %s\n", tdb_file);
4134 if (!strncmp(argv[1], "0x", 2)) {
4135 key = hextodata(tmp_ctx, argv[1] + 2);
4136 if (key.dsize == 0) {
4137 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4141 key.dptr = discard_const(argv[1]);
4142 key.dsize = strlen(argv[1]);
4145 if (!strncmp(argv[2], "0x", 2)) {
4146 value = hextodata(tmp_ctx, argv[2] + 2);
4147 if (value.dsize == 0) {
4148 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4152 value.dptr = discard_const(argv[2]);
4153 value.dsize = strlen(argv[2]);
4156 ZERO_STRUCT(header);
4158 header.rsn = atoll(argv[3]);
4161 header.dmaster = atoi(argv[4]);
4164 header.flags = atoi(argv[5]);
4167 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4168 data.dptr = talloc_size(tmp_ctx, data.dsize);
4169 if (data.dptr == NULL) {
4170 printf("Failed to allocate header+value\n");
4174 *(struct ctdb_ltdb_header *)data.dptr = header;
4175 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4177 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4178 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4185 talloc_free(tmp_ctx);
4190 write a record to a persistent database
4192 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4194 const char *db_name;
4195 struct ctdb_db_context *ctdb_db;
4196 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4197 struct ctdb_transaction_handle *h;
4203 talloc_free(tmp_ctx);
4207 fd = open(argv[2], O_RDONLY);
4209 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4210 talloc_free(tmp_ctx);
4214 ret = fstat(fd, &st);
4216 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4218 talloc_free(tmp_ctx);
4222 if (!S_ISREG(st.st_mode)) {
4223 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4225 talloc_free(tmp_ctx);
4229 data.dsize = st.st_size;
4230 if (data.dsize == 0) {
4233 data.dptr = talloc_size(tmp_ctx, data.dsize);
4234 if (data.dptr == NULL) {
4235 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4237 talloc_free(tmp_ctx);
4240 ret = sys_read(fd, data.dptr, data.dsize);
4241 if (ret != data.dsize) {
4242 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4244 talloc_free(tmp_ctx);
4253 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4254 if (ctdb_db == NULL) {
4255 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4256 talloc_free(tmp_ctx);
4260 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4262 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4263 talloc_free(tmp_ctx);
4267 key.dptr = discard_const(argv[1]);
4268 key.dsize = strlen(argv[1]);
4269 ret = ctdb_transaction_store(h, key, data);
4271 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4272 talloc_free(tmp_ctx);
4276 ret = ctdb_transaction_commit(h);
4278 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4279 talloc_free(tmp_ctx);
4284 talloc_free(tmp_ctx);
4289 * delete a record from a persistent database
4291 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4293 const char *db_name;
4294 struct ctdb_db_context *ctdb_db;
4295 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4296 struct ctdb_transaction_handle *h;
4303 talloc_free(tmp_ctx);
4307 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4308 talloc_free(tmp_ctx);
4312 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4314 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4315 talloc_free(tmp_ctx);
4319 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4320 if (ctdb_db == NULL) {
4321 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4322 talloc_free(tmp_ctx);
4326 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4328 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4329 talloc_free(tmp_ctx);
4333 key.dptr = discard_const(argv[1]);
4334 key.dsize = strlen(argv[1]);
4335 ret = ctdb_transaction_store(h, key, tdb_null);
4337 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4338 talloc_free(tmp_ctx);
4342 ret = ctdb_transaction_commit(h);
4344 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4345 talloc_free(tmp_ctx);
4349 talloc_free(tmp_ctx);
4353 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4358 const char *ret; /* Next byte after successfully parsed value */
4360 /* Error, unless someone says otherwise */
4362 /* Indicates no value to parse */
4365 /* Skip whitespace */
4366 n = strspn(s, " \t");
4370 /* Quoted ASCII string - no wide characters! */
4372 n = strcspn(t, "\"");
4376 data->dptr = talloc_memdup(mem_ctx, t, n);
4377 CTDB_NOMEM_ABORT(data->dptr);
4381 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4384 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4390 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4391 TDB_DATA *key, TDB_DATA *value)
4393 char line [1024]; /* FIXME: make this more flexible? */
4397 ptr = fgets(line, sizeof(line), file);
4404 t = ptrans_parse_string(mem_ctx, line, key);
4405 if (t == NULL || key->dptr == NULL) {
4406 /* Line Ignored but not EOF */
4411 t = ptrans_parse_string(mem_ctx, t, value);
4413 /* Line Ignored but not EOF */
4414 talloc_free(key->dptr);
4423 * Update a persistent database as per file/stdin
4425 static int control_ptrans(struct ctdb_context *ctdb,
4426 int argc, const char **argv)
4428 const char *db_name;
4429 struct ctdb_db_context *ctdb_db;
4430 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4431 struct ctdb_transaction_handle *h;
4432 TDB_DATA key, value;
4437 talloc_free(tmp_ctx);
4443 file = fopen(argv[1], "r");
4445 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4446 talloc_free(tmp_ctx);
4453 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4454 if (ctdb_db == NULL) {
4455 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4459 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4461 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4465 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4466 if (key.dsize != 0) {
4467 ret = ctdb_transaction_store(h, key, value);
4468 /* Minimise memory use */
4469 talloc_free(key.dptr);
4470 if (value.dptr != NULL) {
4471 talloc_free(value.dptr);
4474 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4475 ctdb_transaction_cancel(h);
4481 ret = ctdb_transaction_commit(h);
4483 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4487 if (file != stdin) {
4490 talloc_free(tmp_ctx);
4494 if (file != stdin) {
4498 talloc_free(tmp_ctx);
4503 check if a service is bound to a port or not
4505 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4510 struct sockaddr_in sin;
4513 printf("Use: ctdb chktcport <port>\n");
4517 port = atoi(argv[0]);
4519 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4521 printf("Failed to open local socket\n");
4525 v = fcntl(s, F_GETFL, 0);
4526 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4527 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4530 bzero(&sin, sizeof(sin));
4531 sin.sin_family = PF_INET;
4532 sin.sin_port = htons(port);
4533 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4536 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4544 /* Reload public IPs on a specified nodes */
4545 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4547 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4553 assert_single_node_only();
4559 /* Determine the nodes where IPs need to be reloaded */
4560 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4561 options.pnn, true, &nodes, &pnn_mode)) {
4567 /* Disable takeover runs on all connected nodes. A reply
4568 * indicating success is needed from each node so all nodes
4569 * will need to be active. This will retry until maxruntime
4570 * is exceeded, hence no error handling.
4572 * A check could be added to not allow reloading of IPs when
4573 * there are disconnected nodes. However, this should
4574 * probably be left up to the administrator.
4576 timeout = LONGTIMEOUT;
4577 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4578 "Disable takeover runs", true);
4580 /* Now tell all the desired nodes to reload their public IPs.
4581 * Keep trying this until it succeeds. This assumes all
4582 * failures are transient, which might not be true...
4584 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4585 nodes, 0, LONGTIMELIMIT(),
4587 NULL, NULL, NULL) != 0) {
4589 ("Unable to reload IPs on some nodes, try again.\n"));
4593 /* It isn't strictly necessary to wait until takeover runs are
4594 * re-enabled but doing so can't hurt.
4597 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4598 "Enable takeover runs", true);
4604 talloc_free(tmp_ctx);
4609 display a list of the databases on a remote ctdb
4611 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4614 struct ctdb_dbid_map *dbmap=NULL;
4616 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4618 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4622 if(options.machinereadable){
4623 printm(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4624 for(i=0;i<dbmap->num;i++){
4632 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4633 dbmap->dbs[i].dbid, ctdb, &path);
4634 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4635 dbmap->dbs[i].dbid, ctdb, &name);
4636 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4637 dbmap->dbs[i].dbid, ctdb, &health);
4638 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4639 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4640 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4641 printm(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4642 dbmap->dbs[i].dbid, name, path,
4643 !!(persistent), !!(sticky),
4644 !!(health), !!(readonly));
4649 printf("Number of databases:%d\n", dbmap->num);
4650 for(i=0;i<dbmap->num;i++){
4658 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4659 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4660 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4661 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4662 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4663 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4664 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4665 dbmap->dbs[i].dbid, name, path,
4666 persistent?" PERSISTENT":"",
4667 sticky?" STICKY":"",
4668 readonly?" READONLY":"",
4669 health?" UNHEALTHY":"");
4676 display the status of a database on a remote ctdb
4678 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4680 const char *db_name;
4690 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4694 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4695 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4696 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4697 db_id, db_name, path,
4698 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4699 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4700 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4701 (health ? health : "OK"));
4707 check if the local node is recmaster or not
4708 it will return 1 if this node is the recmaster and 0 if it is not
4709 or if the local ctdb daemon could not be contacted
4711 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4713 uint32_t mypnn, recmaster;
4716 assert_single_node_only();
4718 mypnn = getpnn(ctdb);
4720 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4722 printf("Failed to get the recmaster\n");
4726 if (recmaster != mypnn) {
4727 printf("this node is not the recmaster\n");
4731 printf("this node is the recmaster\n");
4738 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4741 struct timeval tv = timeval_current();
4742 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4744 printf("Unable to get ping response from node %u\n", options.pnn);
4747 printf("response from %u time=%.6f sec (%d clients)\n",
4748 options.pnn, timeval_elapsed(&tv), ret);
4755 get a node's runstate
4757 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4760 enum ctdb_runstate runstate;
4762 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4764 printf("Unable to get runstate response from node %u\n",
4769 enum ctdb_runstate t;
4771 for (i=0; i<argc; i++) {
4773 t = runstate_from_string(argv[i]);
4774 if (t == CTDB_RUNSTATE_UNKNOWN) {
4775 printf("Invalid run state (%s)\n", argv[i]);
4779 if (t == runstate) {
4786 printf("CTDB not in required run state (got %s)\n",
4787 runstate_to_string((enum ctdb_runstate)runstate));
4792 printf("%s\n", runstate_to_string(runstate));
4800 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4811 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4813 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4817 printf("%-23s = %u\n", name, value);
4824 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4835 value = strtoul(argv[1], NULL, 0);
4837 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4839 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4848 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4854 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4856 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4860 for (i=0;i<count;i++) {
4861 control_getvar(ctdb, 1, &list[i]);
4870 display debug level on a node
4872 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4877 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4879 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4882 const char *desc = get_debug_by_level(level);
4884 /* This should never happen */
4887 if (options.machinereadable){
4888 printm(":Name:Level:\n");
4889 printm(":%s:%d:\n", desc, level);
4891 printf("Node %u is at debug level %s (%d)\n",
4892 options.pnn, desc, level);
4899 display reclock file of a node
4901 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4904 const char *reclock;
4906 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4908 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4911 if (options.machinereadable){
4912 if (reclock != NULL) {
4913 printm("%s", reclock);
4916 if (reclock == NULL) {
4917 printf("No reclock file used.\n");
4919 printf("Reclock file:%s\n", reclock);
4927 set the reclock file of a node
4929 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4932 const char *reclock;
4936 } else if (argc == 1) {
4942 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4944 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4951 set the natgw state on/off
4953 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4956 uint32_t natgwstate;
4962 if (!strcmp(argv[0], "on")) {
4964 } else if (!strcmp(argv[0], "off")) {
4970 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4972 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4980 set the lmaster role on/off
4982 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4985 uint32_t lmasterrole;
4991 if (!strcmp(argv[0], "on")) {
4993 } else if (!strcmp(argv[0], "off")) {
4999 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
5001 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
5009 set the recmaster role on/off
5011 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5014 uint32_t recmasterrole;
5020 if (!strcmp(argv[0], "on")) {
5022 } else if (!strcmp(argv[0], "off")) {
5028 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5030 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5038 set debug level on a node or all nodes
5040 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5046 printf("You must specify the debug level. Valid levels are:\n");
5047 print_debug_levels(stdout);
5051 if (!parse_debug(argv[0], &level)) {
5052 printf("Invalid debug level, must be one of\n");
5053 print_debug_levels(stdout);
5057 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5059 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5068 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5074 priority = strtol(argv[0], NULL, 0);
5078 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5080 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5082 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5089 attach to a database
5091 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5093 const char *db_name;
5094 struct ctdb_db_context *ctdb_db;
5095 bool persistent = false;
5105 if (strcmp(argv[1], "persistent") != 0) {
5111 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5112 if (ctdb_db == NULL) {
5113 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5121 * detach from a database
5123 static int control_detach(struct ctdb_context *ctdb, int argc,
5128 int ret, i, status = 0;
5129 struct ctdb_node_map *nodemap = NULL;
5130 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5137 assert_single_node_only();
5139 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5142 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5143 "when recovery is active\n"));
5144 talloc_free(tmp_ctx);
5148 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5151 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5153 talloc_free(tmp_ctx);
5157 for (i=0; i<nodemap->num; i++) {
5160 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5164 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5168 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5169 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5170 "inactive (stopped or banned) node "
5171 "%u\n", nodemap->nodes[i].pnn));
5172 talloc_free(tmp_ctx);
5176 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5177 nodemap->nodes[i].pnn,
5178 "AllowClientDBAttach",
5181 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5182 "AllowClientDBAttach from node %u\n",
5183 nodemap->nodes[i].pnn));
5184 talloc_free(tmp_ctx);
5189 DEBUG(DEBUG_ERR, ("Database access is still active on "
5190 "node %u. Set AllowClientDBAttach=0 "
5192 nodemap->nodes[i].pnn));
5193 talloc_free(tmp_ctx);
5198 talloc_free(tmp_ctx);
5200 for (i=0; i<argc; i++) {
5201 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5205 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5206 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5207 "cannot be detached\n", argv[i]));
5212 ret = ctdb_detach(ctdb, db_id);
5214 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5226 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5228 struct ctdb_db_priority db_prio;
5235 db_prio.db_id = strtoul(argv[0], NULL, 0);
5236 db_prio.priority = strtoul(argv[1], NULL, 0);
5238 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5240 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5250 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5252 uint32_t db_id, priority;
5259 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5263 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5265 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5269 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5275 set the sticky records capability for a database
5277 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5279 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5287 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5291 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5293 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5294 talloc_free(tmp_ctx);
5298 talloc_free(tmp_ctx);
5303 set the readonly capability for a database
5305 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5307 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5315 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5319 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5321 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5322 talloc_free(tmp_ctx);
5326 talloc_free(tmp_ctx);
5333 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5343 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5347 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5349 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5353 printf("Sequence number:%lld\n", (long long)seqnum);
5359 run an eventscript on a node
5361 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5367 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5370 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5374 data.dptr = (unsigned char *)discard_const(argv[0]);
5375 data.dsize = strlen((char *)data.dptr) + 1;
5377 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5379 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5380 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5381 if (ret != 0 || res != 0) {
5382 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5383 talloc_free(tmp_ctx);
5386 talloc_free(tmp_ctx);
5390 #define DB_VERSION 1
5391 #define MAX_DB_NAME 64
5392 struct db_file_header {
5393 unsigned long version;
5395 unsigned long persistent;
5397 const char name[MAX_DB_NAME];
5400 struct backup_data {
5401 struct ctdb_marshall_buffer *records;
5404 bool traverse_error;
5407 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5409 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5410 struct ctdb_rec_data *rec;
5412 /* add the record */
5413 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5415 bd->traverse_error = true;
5416 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5419 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5420 if (bd->records == NULL) {
5421 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5422 bd->traverse_error = true;
5425 bd->records->count++;
5426 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5427 bd->len += rec->length;
5435 * backup a database to a file
5437 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5439 const char *db_name;
5441 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5442 struct db_file_header dbhdr;
5443 struct ctdb_db_context *ctdb_db;
5444 struct backup_data *bd;
5447 const char *reason = NULL;
5451 assert_single_node_only();
5454 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5458 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5462 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5463 db_id, tmp_ctx, &reason);
5465 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5467 talloc_free(tmp_ctx);
5471 uint32_t allow_unhealthy = 0;
5473 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5474 "AllowUnhealthyDBRead",
5477 if (allow_unhealthy != 1) {
5478 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5481 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5483 talloc_free(tmp_ctx);
5487 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5489 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5490 "tunnable AllowUnhealthyDBRead = %u\n",
5494 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5495 if (ctdb_db == NULL) {
5496 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5497 talloc_free(tmp_ctx);
5502 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5504 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5505 talloc_free(tmp_ctx);
5510 bd = talloc_zero(tmp_ctx, struct backup_data);
5512 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5513 talloc_free(tmp_ctx);
5517 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5518 if (bd->records == NULL) {
5519 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5520 talloc_free(tmp_ctx);
5524 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5525 bd->records->db_id = ctdb_db->db_id;
5526 /* traverse the database collecting all records */
5527 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5528 bd->traverse_error) {
5529 DEBUG(DEBUG_ERR,("Traverse error\n"));
5530 talloc_free(tmp_ctx);
5534 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5537 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5539 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5540 talloc_free(tmp_ctx);
5545 dbhdr.version = DB_VERSION;
5546 dbhdr.timestamp = time(NULL);
5547 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5548 dbhdr.size = bd->len;
5549 if (strlen(argv[0]) >= MAX_DB_NAME) {
5550 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5553 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5554 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
5556 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5559 ret = sys_write(fh, bd->records, bd->len);
5561 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5570 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5574 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5576 talloc_free(tmp_ctx);
5581 * restore a database from a file
5583 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5586 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5589 struct db_file_header dbhdr;
5590 struct ctdb_db_context *ctdb_db;
5591 struct ctdb_node_map *nodemap=NULL;
5592 struct ctdb_vnn_map *vnnmap=NULL;
5594 struct ctdb_control_wipe_database w;
5596 uint32_t generation;
5601 assert_single_node_only();
5603 if (argc < 1 || argc > 2) {
5604 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5608 fh = open(argv[0], O_RDONLY);
5610 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5611 talloc_free(tmp_ctx);
5615 sys_read(fh, &dbhdr, sizeof(dbhdr));
5616 if (dbhdr.version != DB_VERSION) {
5617 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5619 talloc_free(tmp_ctx);
5623 dbname = discard_const(dbhdr.name);
5625 dbname = discard_const(argv[1]);
5628 outdata.dsize = dbhdr.size;
5629 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5630 if (outdata.dptr == NULL) {
5631 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5633 talloc_free(tmp_ctx);
5636 sys_read(fh, outdata.dptr, outdata.dsize);
5639 tm = localtime(&dbhdr.timestamp);
5640 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5641 printf("Restoring database '%s' from backup @ %s\n",
5645 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5646 if (ctdb_db == NULL) {
5647 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5648 talloc_free(tmp_ctx);
5652 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5654 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5655 talloc_free(tmp_ctx);
5660 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5662 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5663 talloc_free(tmp_ctx);
5667 /* freeze all nodes */
5668 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5669 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5670 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5676 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5677 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5678 talloc_free(tmp_ctx);
5683 generation = vnnmap->generation;
5684 data.dptr = (void *)&generation;
5685 data.dsize = sizeof(generation);
5687 /* start a cluster wide transaction */
5688 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5689 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5691 TIMELIMIT(), false, data,
5694 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5699 w.db_id = ctdb_db->db_id;
5700 w.transaction_id = generation;
5702 data.dptr = (void *)&w;
5703 data.dsize = sizeof(w);
5705 /* wipe all the remote databases. */
5706 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5707 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5709 TIMELIMIT(), false, data,
5712 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5713 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5714 talloc_free(tmp_ctx);
5718 /* push the database */
5719 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5720 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5722 TIMELIMIT(), false, outdata,
5725 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5726 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5727 talloc_free(tmp_ctx);
5731 data.dptr = (void *)&ctdb_db->db_id;
5732 data.dsize = sizeof(ctdb_db->db_id);
5734 /* mark the database as healthy */
5735 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5736 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5738 TIMELIMIT(), false, data,
5741 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5742 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5743 talloc_free(tmp_ctx);
5747 data.dptr = (void *)&generation;
5748 data.dsize = sizeof(generation);
5750 /* commit all the changes */
5751 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5753 TIMELIMIT(), false, data,
5756 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5757 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5758 talloc_free(tmp_ctx);
5763 /* thaw all nodes */
5764 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5765 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5771 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5772 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5773 talloc_free(tmp_ctx);
5778 talloc_free(tmp_ctx);
5783 * dump a database backup from a file
5785 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5787 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5789 struct db_file_header dbhdr;
5793 struct ctdb_rec_data *rec = NULL;
5794 struct ctdb_marshall_buffer *m;
5795 struct ctdb_dump_db_context c;
5797 assert_single_node_only();
5800 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5804 fh = open(argv[0], O_RDONLY);
5806 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5807 talloc_free(tmp_ctx);
5811 sys_read(fh, &dbhdr, sizeof(dbhdr));
5812 if (dbhdr.version != DB_VERSION) {
5813 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5815 talloc_free(tmp_ctx);
5819 outdata.dsize = dbhdr.size;
5820 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5821 if (outdata.dptr == NULL) {
5822 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5824 talloc_free(tmp_ctx);
5827 sys_read(fh, outdata.dptr, outdata.dsize);
5829 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5831 tm = localtime(&dbhdr.timestamp);
5832 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5833 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5834 dbhdr.name, m->db_id, tbuf);
5838 c.printemptyrecords = (bool)options.printemptyrecords;
5839 c.printdatasize = (bool)options.printdatasize;
5840 c.printlmaster = false;
5841 c.printhash = (bool)options.printhash;
5842 c.printrecordflags = (bool)options.printrecordflags;
5844 for (i=0; i < m->count; i++) {
5848 /* we do not want the header splitted, so we pass NULL*/
5849 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5852 ctdb_dumpdb_record(ctdb, key, data, &c);
5855 printf("Dumped %d records\n", i);
5856 talloc_free(tmp_ctx);
5861 * wipe a database from a file
5863 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5866 const char *db_name;
5868 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5870 struct ctdb_db_context *ctdb_db;
5871 struct ctdb_node_map *nodemap = NULL;
5872 struct ctdb_vnn_map *vnnmap = NULL;
5874 struct ctdb_control_wipe_database w;
5876 uint32_t generation;
5879 assert_single_node_only();
5882 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5886 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5890 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5891 if (ctdb_db == NULL) {
5892 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5894 talloc_free(tmp_ctx);
5898 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5901 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5903 talloc_free(tmp_ctx);
5907 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5910 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5912 talloc_free(tmp_ctx);
5916 /* freeze all nodes */
5917 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5918 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5919 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5926 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5927 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5928 CTDB_RECOVERY_ACTIVE);
5929 talloc_free(tmp_ctx);
5934 generation = vnnmap->generation;
5935 data.dptr = (void *)&generation;
5936 data.dsize = sizeof(generation);
5938 /* start a cluster wide transaction */
5939 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5940 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5942 TIMELIMIT(), false, data,
5946 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5947 "transactions.\n"));
5951 w.db_id = ctdb_db->db_id;
5952 w.transaction_id = generation;
5954 data.dptr = (void *)&w;
5955 data.dsize = sizeof(w);
5957 /* wipe all the remote databases. */
5958 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5959 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5961 TIMELIMIT(), false, data,
5964 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5965 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5966 talloc_free(tmp_ctx);
5970 data.dptr = (void *)&ctdb_db->db_id;
5971 data.dsize = sizeof(ctdb_db->db_id);
5973 /* mark the database as healthy */
5974 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5975 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5977 TIMELIMIT(), false, data,
5980 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5981 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5982 talloc_free(tmp_ctx);
5986 data.dptr = (void *)&generation;
5987 data.dsize = sizeof(generation);
5989 /* commit all the changes */
5990 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5992 TIMELIMIT(), false, data,
5995 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5996 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5997 talloc_free(tmp_ctx);
6001 /* thaw all nodes */
6002 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6003 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
6009 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
6010 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6011 talloc_free(tmp_ctx);
6015 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
6017 talloc_free(tmp_ctx);
6024 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6030 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6031 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6032 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6033 if (ret != 0 || res != 0) {
6034 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6035 talloc_free(tmp_ctx);
6038 sys_write(1, data.dptr, data.dsize);
6039 talloc_free(tmp_ctx);
6044 handler for memory dumps
6046 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6047 TDB_DATA data, void *private_data)
6049 sys_write(1, data.dptr, data.dsize);
6054 dump memory usage on the recovery daemon
6056 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6060 struct srvid_request rd;
6062 rd.pnn = ctdb_get_pnn(ctdb);
6063 rd.srvid = getpid();
6065 /* register a message port for receiveing the reply so that we
6066 can receive the reply
6068 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6071 data.dptr = (uint8_t *)&rd;
6072 data.dsize = sizeof(rd);
6074 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6076 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6080 /* this loop will terminate when we have received the reply */
6082 event_loop_once(ctdb->ev);
6089 send a message to a srvid
6091 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6093 unsigned long srvid;
6101 srvid = strtoul(argv[0], NULL, 0);
6103 data.dptr = (uint8_t *)discard_const(argv[1]);
6104 data.dsize= strlen(argv[1]);
6106 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6108 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6116 handler for msglisten
6118 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6119 TDB_DATA data, void *private_data)
6123 printf("Message received: ");
6124 for (i=0;i<data.dsize;i++) {
6125 printf("%c", data.dptr[i]);
6131 listen for messages on a messageport
6133 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6139 /* register a message port and listen for messages
6141 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6142 printf("Listening for messages on srvid:%d\n", (int)srvid);
6145 event_loop_once(ctdb->ev);
6152 list all nodes in the cluster
6153 we parse the nodes file directly
6155 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6157 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6158 struct pnn_node *pnn_nodes;
6159 struct pnn_node *pnn_node;
6161 assert_single_node_only();
6163 pnn_nodes = read_nodes_file(mem_ctx);
6164 if (pnn_nodes == NULL) {
6165 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6166 talloc_free(mem_ctx);
6170 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6171 const char *addr = ctdb_addr_to_str(&pnn_node->addr);
6172 if (options.machinereadable){
6173 printm(":%d:%s:\n", pnn_node->pnn, addr);
6175 printf("%s\n", addr);
6178 talloc_free(mem_ctx);
6184 reload the nodes file on the local node
6186 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6190 struct ctdb_node_map *nodemap=NULL;
6192 assert_single_node_only();
6194 mypnn = ctdb_get_pnn(ctdb);
6196 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6198 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6202 /* reload the nodes file on all remote nodes */
6203 for (i=0;i<nodemap->num;i++) {
6204 if (nodemap->nodes[i].pnn == mypnn) {
6207 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6208 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6209 nodemap->nodes[i].pnn);
6211 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6215 /* reload the nodes file on the local node */
6216 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6217 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6219 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6222 /* initiate a recovery */
6223 control_recover(ctdb, argc, argv);
6229 static const struct {
6231 int (*fn)(struct ctdb_context *, int, const char **);
6233 bool without_daemon; /* can be run without daemon running ? */
6236 } ctdb_commands[] = {
6237 { "version", control_version, true, true, "show version of ctdb" },
6238 { "status", control_status, true, false, "show node status" },
6239 { "uptime", control_uptime, true, false, "show node uptime" },
6240 { "ping", control_ping, true, false, "ping all nodes" },
6241 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6242 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6243 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6244 { "listvars", control_listvars, true, false, "list tunable variables"},
6245 { "statistics", control_statistics, false, false, "show statistics" },
6246 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6247 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6248 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6249 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6250 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6251 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6252 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6253 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6254 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6255 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6256 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6257 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6258 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6259 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6260 { "lvs", control_lvs, true, false, "show lvs configuration" },
6261 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6262 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6263 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6264 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6265 { "getdebug", control_getdebug, true, false, "get debug level" },
6266 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6267 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6268 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6269 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6270 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6271 { "disable", control_disable, true, false, "disable a nodes public IP" },
6272 { "enable", control_enable, true, false, "enable a nodes public IP" },
6273 { "stop", control_stop, true, false, "stop a node" },
6274 { "continue", control_continue, true, false, "re-start a stopped node" },
6275 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6276 { "unban", control_unban, true, false, "unban a node" },
6277 { "showban", control_showban, true, false, "show ban information"},
6278 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6279 { "recover", control_recover, true, false, "force recovery" },
6280 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6281 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6282 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6283 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6284 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6285 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6286 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6287 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6288 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6290 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6292 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6293 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6294 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6295 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6296 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6297 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6298 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6299 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6300 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6301 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6302 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6303 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6304 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6305 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6306 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6307 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6308 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6309 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6310 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6311 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6312 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6313 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6314 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6315 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6316 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6317 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6318 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6319 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6320 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6321 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6322 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6323 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6324 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6325 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6326 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6327 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6328 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6329 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6330 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6331 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6332 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6333 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6334 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6335 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6336 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6337 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6338 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6339 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6340 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6346 static void usage(void)
6350 "Usage: ctdb [options] <control>\n" \
6352 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6353 " -Y generate machine readable output\n"
6354 " -x <char> specify delimiter for machine readable output\n"
6355 " -v generate verbose output\n"
6356 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6357 printf("Controls:\n");
6358 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6359 printf(" %-15s %-27s %s\n",
6360 ctdb_commands[i].name,
6361 ctdb_commands[i].args?ctdb_commands[i].args:"",
6362 ctdb_commands[i].msg);
6368 static void ctdb_alarm(int sig)
6370 printf("Maximum runtime exceeded - exiting\n");
6377 int main(int argc, const char *argv[])
6379 struct ctdb_context *ctdb;
6380 char *nodestring = NULL;
6381 struct poptOption popt_options[] = {
6384 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6385 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6386 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machine readable output", NULL },
6387 { NULL, 'x', POPT_ARG_STRING, &options.machineseparator, 0, "specify separator for machine readable output", "char" },
6388 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6389 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6390 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6391 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6392 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6393 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6394 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6398 const char **extra_argv;
6402 struct event_context *ev;
6403 const char *control;
6407 /* set some defaults */
6408 options.maxruntime = 0;
6409 options.timelimit = 10;
6410 options.pnn = CTDB_CURRENT_NODE;
6412 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6414 while ((opt = poptGetNextOpt(pc)) != -1) {
6417 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6418 poptBadOption(pc, 0), poptStrerror(opt)));
6423 /* setup the remaining options for the main program to use */
6424 extra_argv = poptGetArgs(pc);
6427 while (extra_argv[extra_argc]) extra_argc++;
6430 if (extra_argc < 1) {
6434 if (options.maxruntime == 0) {
6435 const char *ctdb_timeout;
6436 ctdb_timeout = getenv("CTDB_TIMEOUT");
6437 if (ctdb_timeout != NULL) {
6438 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6440 /* default timeout is 120 seconds */
6441 options.maxruntime = 120;
6445 if (options.machineseparator != NULL) {
6446 if (strlen(options.machineseparator) != 1) {
6447 printf("Invalid separator \"%s\" - "
6448 "must be single character\n",
6449 options.machineseparator);
6454 options.machinereadable = true;
6455 } else if (options.machinereadable) {
6456 options.machineseparator = ":";
6459 signal(SIGALRM, ctdb_alarm);
6460 alarm(options.maxruntime);
6462 control = extra_argv[0];
6464 /* Default value for CTDB_BASE - don't override */
6465 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6467 ev = event_context_init(NULL);
6469 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6473 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6474 if (strcmp(control, ctdb_commands[i].name) == 0) {
6479 if (i == ARRAY_SIZE(ctdb_commands)) {
6480 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6484 if (ctdb_commands[i].without_daemon == true) {
6485 if (nodestring != NULL) {
6486 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6489 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6492 /* initialise ctdb */
6493 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6497 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6499 pnn = find_node_xpnn();
6502 ("Is this node part of a CTDB cluster?\n"));
6507 /* setup the node number(s) to contact */
6508 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6509 &options.nodes, &options.pnn)) {
6513 if (options.pnn == CTDB_CURRENT_NODE) {
6514 options.pnn = options.nodes[0];
6517 if (ctdb_commands[i].auto_all &&
6518 ((options.pnn == CTDB_BROADCAST_ALL) ||
6519 (options.pnn == CTDB_MULTICAST))) {
6523 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6524 options.pnn = options.nodes[j];
6525 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6528 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6533 (void)poptFreeContext(pc);