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);
852 static struct ctdb_node_map *read_nodes_file(TALLOC_CTX *mem_ctx)
854 const char *nodes_list;
856 /* read the nodes file */
857 nodes_list = getenv("CTDB_NODES");
858 if (nodes_list == NULL) {
859 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
860 getenv("CTDB_BASE"));
861 if (nodes_list == NULL) {
862 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
867 return ctdb_read_nodes_file(mem_ctx, nodes_list);
871 show the PNN of the current node
872 discover the pnn by loading the nodes file and try to bind to all
873 addresses one at a time until the ip address is found.
875 static int find_node_xpnn(void)
877 TALLOC_CTX *mem_ctx = talloc_new(NULL);
878 struct ctdb_node_map *node_map;
881 node_map = read_nodes_file(mem_ctx);
882 if (node_map == NULL) {
883 talloc_free(mem_ctx);
887 for (i = 0; i < node_map->num; i++) {
888 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
891 if (ctdb_sys_have_ip(&node_map->nodes[i].addr)) {
892 pnn = node_map->nodes[i].pnn;
893 talloc_free(mem_ctx);
898 printf("Failed to detect which PNN this node is\n");
899 talloc_free(mem_ctx);
903 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
907 assert_single_node_only();
909 pnn = find_node_xpnn();
914 printf("PNN:%d\n", pnn);
918 /* Helpers for ctdb status
920 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
922 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
926 if (node->flags == 0) {
927 struct ctdb_control_get_ifaces *ifaces;
929 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
930 tmp_ctx, &ifaces) == 0) {
931 for (j=0; j < ifaces->num; j++) {
932 if (ifaces->ifaces[j].link_state != 0) {
940 talloc_free(tmp_ctx);
945 static void control_status_header_machine(void)
947 printm(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
948 ":Inactive:PartiallyOnline:ThisNode:\n");
951 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
952 struct ctdb_node_and_flags *node)
954 printm(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
955 ctdb_addr_to_str(&node->addr),
956 !!(node->flags&NODE_FLAGS_DISCONNECTED),
957 !!(node->flags&NODE_FLAGS_BANNED),
958 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
959 !!(node->flags&NODE_FLAGS_UNHEALTHY),
960 !!(node->flags&NODE_FLAGS_STOPPED),
961 !!(node->flags&NODE_FLAGS_INACTIVE),
962 is_partially_online(ctdb, node) ? 1 : 0,
963 (node->pnn == mypnn)?'Y':'N');
968 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
969 struct ctdb_node_and_flags *node)
971 printf("pnn:%d %-16s %s%s\n", node->pnn,
972 ctdb_addr_to_str(&node->addr),
973 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
974 node->pnn == mypnn?" (THIS NODE)":"");
980 display remote ctdb status
982 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
984 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
986 struct ctdb_vnn_map *vnnmap=NULL;
987 struct ctdb_node_map *nodemap=NULL;
988 uint32_t recmode, recmaster, mypnn;
989 int num_deleted_nodes = 0;
992 mypnn = getpnn(ctdb);
994 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
996 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
997 talloc_free(tmp_ctx);
1001 if (options.machinereadable) {
1002 control_status_header_machine();
1003 for (i=0;i<nodemap->num;i++) {
1004 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1007 (void) control_status_1_machine(ctdb, mypnn,
1008 &nodemap->nodes[i]);
1010 talloc_free(tmp_ctx);
1014 for (i=0; i<nodemap->num; i++) {
1015 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1016 num_deleted_nodes++;
1019 if (num_deleted_nodes == 0) {
1020 printf("Number of nodes:%d\n", nodemap->num);
1022 printf("Number of nodes:%d (including %d deleted nodes)\n",
1023 nodemap->num, num_deleted_nodes);
1025 for(i=0;i<nodemap->num;i++){
1026 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1029 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1032 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1034 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1035 talloc_free(tmp_ctx);
1038 if (vnnmap->generation == INVALID_GENERATION) {
1039 printf("Generation:INVALID\n");
1041 printf("Generation:%d\n",vnnmap->generation);
1043 printf("Size:%d\n",vnnmap->size);
1044 for(i=0;i<vnnmap->size;i++){
1045 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1048 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1050 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1051 talloc_free(tmp_ctx);
1054 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1056 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1058 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1059 talloc_free(tmp_ctx);
1062 printf("Recovery master:%d\n",recmaster);
1064 talloc_free(tmp_ctx);
1068 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1070 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1072 struct ctdb_node_map *nodemap=NULL;
1074 uint32_t pnn_mode, mypnn;
1080 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1081 options.pnn, true, &nodes, &pnn_mode)) {
1085 if (options.machinereadable) {
1086 control_status_header_machine();
1087 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1088 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1091 mypnn = getpnn(ctdb);
1093 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1095 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1096 talloc_free(tmp_ctx);
1102 for (i = 0; i < talloc_array_length(nodes); i++) {
1103 if (options.machinereadable) {
1104 ret |= control_status_1_machine(ctdb, mypnn,
1105 &nodemap->nodes[nodes[i]]);
1107 ret |= control_status_1_human(ctdb, mypnn,
1108 &nodemap->nodes[nodes[i]]);
1112 talloc_free(tmp_ctx);
1116 static struct ctdb_node_map *read_natgw_nodes_file(struct ctdb_context *ctdb,
1117 TALLOC_CTX *mem_ctx)
1119 const char *natgw_list;
1120 struct ctdb_node_map *natgw_nodes = NULL;
1122 natgw_list = getenv("CTDB_NATGW_NODES");
1123 if (natgw_list == NULL) {
1124 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1125 getenv("CTDB_BASE"));
1126 if (natgw_list == NULL) {
1127 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1131 /* The PNNs/flags will be junk but they're not used */
1132 natgw_nodes = ctdb_read_nodes_file(mem_ctx, natgw_list);
1133 if (natgw_nodes == NULL) {
1135 ("Failed to load natgw node list '%s'\n", natgw_list));
1141 /* talloc off the existing nodemap... */
1142 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1144 return talloc_zero_size(nodemap,
1145 offsetof(struct ctdb_node_map, nodes) +
1146 nodemap->num * sizeof(struct ctdb_node_and_flags));
1149 static struct ctdb_node_map *
1150 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1151 struct ctdb_node_map *nodemap,
1152 struct ctdb_node_map *natgw_nodes)
1155 struct ctdb_node_map *ret;
1157 ret = talloc_nodemap(nodemap);
1158 CTDB_NO_MEMORY_NULL(ctdb, ret);
1162 for (i = 0; i < nodemap->num; i++) {
1163 for(j = 0; j < natgw_nodes->num ; j++) {
1164 if (nodemap->nodes[j].flags & NODE_FLAGS_DELETED) {
1167 if (ctdb_same_ip(&natgw_nodes->nodes[j].addr,
1168 &nodemap->nodes[i].addr)) {
1170 ret->nodes[ret->num] = nodemap->nodes[i];
1180 static struct ctdb_node_map *
1181 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1182 struct ctdb_node_map *nodemap,
1183 uint32_t required_capabilities,
1187 uint32_t capabilities;
1188 struct ctdb_node_map *ret;
1190 ret = talloc_nodemap(nodemap);
1191 CTDB_NO_MEMORY_NULL(ctdb, ret);
1195 for (i = 0; i < nodemap->num; i++) {
1198 /* Disconnected nodes have no capabilities! */
1199 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1203 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1204 nodemap->nodes[i].pnn,
1207 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1208 nodemap->nodes[i].pnn));
1212 if (!(capabilities & required_capabilities)) {
1216 ret->nodes[ret->num] = nodemap->nodes[i];
1226 static struct ctdb_node_map *
1227 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1228 struct ctdb_node_map *nodemap,
1229 uint32_t flags_mask)
1232 struct ctdb_node_map *ret;
1234 ret = talloc_nodemap(nodemap);
1235 CTDB_NO_MEMORY_NULL(ctdb, ret);
1239 for (i = 0; i < nodemap->num; i++) {
1240 if (nodemap->nodes[i].flags & flags_mask) {
1244 ret->nodes[ret->num] = nodemap->nodes[i];
1252 display the list of nodes belonging to this natgw configuration
1254 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1256 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1258 struct ctdb_node_map *natgw_nodes = NULL;
1259 struct ctdb_node_map *orig_nodemap=NULL;
1260 struct ctdb_node_map *nodemap;
1261 uint32_t mypnn, pnn;
1264 /* When we have some nodes that could be the NATGW, make a
1265 * series of attempts to find the first node that doesn't have
1266 * certain status flags set.
1268 uint32_t exclude_flags[] = {
1269 /* Look for a nice healthy node */
1270 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1271 /* If not found, an UNHEALTHY/BANNED node will do */
1272 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1273 /* If not found, a STOPPED node will do */
1274 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1278 /* read the natgw nodes file into a linked list */
1279 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1280 if (natgw_nodes == NULL) {
1285 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1286 tmp_ctx, &orig_nodemap);
1288 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1289 talloc_free(tmp_ctx);
1293 /* Get a nodemap that includes only the nodes in the NATGW
1295 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1296 if (nodemap == NULL) {
1301 ret = 2; /* matches ENOENT */
1304 /* For each flag mask... */
1305 for (i = 0; exclude_flags[i] != 0; i++) {
1306 /* ... get a nodemap that excludes nodes with with
1307 * masked flags... */
1308 struct ctdb_node_map *t =
1309 filter_nodemap_by_flags(ctdb, nodemap,
1317 /* ... and find the first node with the NATGW
1319 struct ctdb_node_map *n;
1320 n = filter_nodemap_by_capabilities(ctdb, t,
1330 pnn = n->nodes[0].pnn;
1331 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1338 if (options.machinereadable) {
1339 printm(":Node:IP:\n");
1340 printm(":%d:%s:\n", pnn, ip);
1342 printf("%d %s\n", pnn, ip);
1345 /* print the pruned list of nodes belonging to this natgw list */
1346 mypnn = getpnn(ctdb);
1347 if (options.machinereadable) {
1348 control_status_header_machine();
1350 printf("Number of nodes:%d\n", nodemap->num);
1352 for(i=0;i<nodemap->num;i++){
1353 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1356 if (options.machinereadable) {
1357 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1359 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1364 talloc_free(tmp_ctx);
1369 display the status of the scripts for monitoring (or other events)
1371 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1372 enum ctdb_eventscript_call type)
1374 struct ctdb_scripts_wire *script_status;
1377 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1379 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1383 if (script_status == NULL) {
1384 if (!options.machinereadable) {
1385 printf("%s cycle never run\n",
1386 ctdb_eventscript_call_names[type]);
1391 if (!options.machinereadable) {
1393 for (i=0; i<script_status->num_scripts; i++) {
1394 if (script_status->scripts[i].status != -ENOEXEC) {
1398 printf("%d scripts were executed last %s cycle\n",
1400 ctdb_eventscript_call_names[type]);
1402 for (i=0; i<script_status->num_scripts; i++) {
1403 const char *status = NULL;
1405 switch (script_status->scripts[i].status) {
1407 status = "TIMEDOUT";
1410 status = "DISABLED";
1416 if (script_status->scripts[i].status > 0)
1420 if (options.machinereadable) {
1421 printm(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1422 ctdb_eventscript_call_names[type],
1423 script_status->scripts[i].name,
1424 script_status->scripts[i].status,
1426 (long)script_status->scripts[i].start.tv_sec,
1427 (long)script_status->scripts[i].start.tv_usec,
1428 (long)script_status->scripts[i].finished.tv_sec,
1429 (long)script_status->scripts[i].finished.tv_usec,
1430 script_status->scripts[i].output);
1434 printf("%-20s Status:%s ",
1435 script_status->scripts[i].name, status);
1437 /* Some other error, eg from stat. */
1438 printf("%-20s Status:CANNOT RUN (%s)",
1439 script_status->scripts[i].name,
1440 strerror(-script_status->scripts[i].status));
1442 if (script_status->scripts[i].status >= 0) {
1443 printf("Duration:%.3lf ",
1444 timeval_delta(&script_status->scripts[i].finished,
1445 &script_status->scripts[i].start));
1447 if (script_status->scripts[i].status != -ENOEXEC) {
1449 ctime(&script_status->scripts[i].start.tv_sec));
1450 if (script_status->scripts[i].status != 0) {
1451 printf(" OUTPUT:%s\n",
1452 script_status->scripts[i].output);
1462 static int control_scriptstatus(struct ctdb_context *ctdb,
1463 int argc, const char **argv)
1466 enum ctdb_eventscript_call type, min, max;
1470 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1475 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1479 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1480 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1486 if (type == CTDB_EVENT_MAX) {
1487 if (strcmp(arg, "all") == 0) {
1489 max = CTDB_EVENT_MAX;
1491 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1496 if (options.machinereadable) {
1497 printm(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1500 for (type = min; type < max; type++) {
1501 ret = control_one_scriptstatus(ctdb, type);
1511 enable an eventscript
1513 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1521 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1523 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1531 disable an eventscript
1533 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1541 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1543 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1551 display the pnn of the recovery master
1553 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1558 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1560 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1563 printf("%d\n",recmaster);
1569 add a tickle to a public address
1571 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1573 struct ctdb_tcp_connection t;
1577 assert_single_node_only();
1583 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1584 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1587 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1588 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1592 data.dptr = (uint8_t *)&t;
1593 data.dsize = sizeof(t);
1595 /* tell all nodes about this tcp connection */
1596 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1597 0, data, ctdb, NULL, NULL, NULL, NULL);
1599 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1608 delete a tickle from a node
1610 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1612 struct ctdb_tcp_connection t;
1616 assert_single_node_only();
1622 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1623 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1626 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1627 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1631 data.dptr = (uint8_t *)&t;
1632 data.dsize = sizeof(t);
1634 /* tell all nodes about this tcp connection */
1635 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1636 0, data, ctdb, NULL, NULL, NULL, NULL);
1638 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1647 get a list of all tickles for this pnn
1649 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1651 struct ctdb_control_tcp_tickle_list *list;
1652 ctdb_sock_addr addr;
1656 assert_single_node_only();
1663 port = atoi(argv[1]);
1666 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1667 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1671 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1673 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1677 if (options.machinereadable){
1678 printm(":source ip:port:destination ip:port:\n");
1679 for (i=0;i<list->tickles.num;i++) {
1680 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1683 printm(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1684 printm(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1687 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1688 printf("Num tickles:%u\n", list->tickles.num);
1689 for (i=0;i<list->tickles.num;i++) {
1690 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1693 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1694 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));
1704 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1706 struct ctdb_all_public_ips *ips;
1707 struct ctdb_public_ip ip;
1710 uint32_t disable_time;
1712 struct ctdb_node_map *nodemap=NULL;
1713 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1716 data.dptr = (uint8_t*)&disable_time;
1717 data.dsize = sizeof(disable_time);
1718 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1720 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1726 /* read the public ip list from the node */
1727 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1729 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1730 talloc_free(tmp_ctx);
1734 for (i=0;i<ips->num;i++) {
1735 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1740 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1741 pnn, ctdb_addr_to_str(addr)));
1742 talloc_free(tmp_ctx);
1749 data.dptr = (uint8_t *)&ip;
1750 data.dsize = sizeof(ip);
1752 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1754 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1755 talloc_free(tmp_ctx);
1759 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1760 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1767 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1768 talloc_free(tmp_ctx);
1772 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1774 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1775 talloc_free(tmp_ctx);
1779 /* update the recovery daemon so it now knows to expect the new
1780 node assignment for this ip.
1782 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1784 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1788 talloc_free(tmp_ctx);
1794 * scans all other nodes and returns a pnn for another node that can host this
1798 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1800 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1801 struct ctdb_all_public_ips *ips;
1802 struct ctdb_node_map *nodemap=NULL;
1806 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1808 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1809 talloc_free(tmp_ctx);
1813 for(i=0;i<nodemap->num;i++){
1814 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1817 if (nodemap->nodes[i].pnn == options.pnn) {
1821 /* read the public ip list from this node */
1822 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1824 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1828 for (j=0;j<ips->num;j++) {
1829 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1830 pnn = nodemap->nodes[i].pnn;
1831 talloc_free(tmp_ctx);
1838 talloc_free(tmp_ctx);
1842 /* If pnn is -1 then try to find a node to move IP to... */
1843 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1845 bool pnn_specified = (pnn == -1 ? false : true);
1848 while (retries < 5) {
1849 if (!pnn_specified) {
1850 pnn = find_other_host_for_public_ip(ctdb, addr);
1855 ("Trying to move public IP to node %u\n", pnn));
1858 if (move_ip(ctdb, addr, pnn) == 0) {
1871 move/failover an ip address to a specific node
1873 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1876 ctdb_sock_addr addr;
1878 assert_single_node_only();
1885 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1886 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1891 if (sscanf(argv[1], "%u", &pnn) != 1) {
1892 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1896 if (!try_moveip(ctdb, &addr, pnn)) {
1897 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1904 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1908 data.dptr = (uint8_t *)&pnn;
1909 data.dsize = sizeof(uint32_t);
1910 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1912 ("Failed to send message to force node %u to be a rebalancing target\n",
1922 rebalance a node by setting it to allow failback and triggering a
1925 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1927 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1932 assert_single_node_only();
1938 /* Determine the nodes where IPs need to be reloaded */
1939 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1940 options.pnn, true, &nodes, &pnn_mode)) {
1945 for (i = 0; i < talloc_array_length(nodes); i++) {
1946 if (!rebalance_node(ctdb, nodes[i])) {
1952 talloc_free(tmp_ctx);
1956 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1958 struct ctdb_public_ip ip;
1961 uint32_t disable_time;
1963 struct ctdb_node_map *nodemap=NULL;
1964 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1967 data.dptr = (uint8_t*)&disable_time;
1968 data.dsize = sizeof(disable_time);
1969 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1971 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1978 data.dptr = (uint8_t *)&ip;
1979 data.dsize = sizeof(ip);
1981 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1983 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1984 talloc_free(tmp_ctx);
1988 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1989 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1996 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1997 talloc_free(tmp_ctx);
2001 talloc_free(tmp_ctx);
2006 release an ip form all nodes and have it re-assigned by recd
2008 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2010 ctdb_sock_addr addr;
2012 assert_single_node_only();
2019 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2020 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2024 if (rebalance_ip(ctdb, &addr) != 0) {
2025 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2032 static int getips_store_callback(void *param, void *data)
2034 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2035 struct ctdb_all_public_ips *ips = param;
2039 ips->ips[i].pnn = node_ip->pnn;
2040 ips->ips[i].addr = node_ip->addr;
2044 static int getips_count_callback(void *param, void *data)
2046 uint32_t *count = param;
2053 static uint32_t *ip_key(ctdb_sock_addr *ip)
2055 static uint32_t key[IP_KEYLEN];
2057 bzero(key, sizeof(key));
2059 switch (ip->sa.sa_family) {
2061 key[0] = ip->ip.sin_addr.s_addr;
2064 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2072 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2079 static void *add_ip_callback(void *parm, void *data)
2085 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2087 struct ctdb_all_public_ips *tmp_ips;
2088 struct ctdb_node_map *nodemap=NULL;
2089 trbt_tree_t *ip_tree;
2093 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2095 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2099 ip_tree = trbt_create(tmp_ctx, 0);
2101 for(i=0;i<nodemap->num;i++){
2102 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2105 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2109 /* read the public ip list from this node */
2110 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2112 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2116 for (j=0; j<tmp_ips->num;j++) {
2117 struct ctdb_public_ip *node_ip;
2119 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2120 node_ip->pnn = tmp_ips->ips[j].pnn;
2121 node_ip->addr = tmp_ips->ips[j].addr;
2123 trbt_insertarray32_callback(ip_tree,
2124 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2128 talloc_free(tmp_ips);
2133 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2135 len = offsetof(struct ctdb_all_public_ips, ips) +
2136 count*sizeof(struct ctdb_public_ip);
2137 tmp_ips = talloc_zero_size(tmp_ctx, len);
2138 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2146 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2148 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2150 event_add_timed(ctdb->ev, ctdb,
2151 timeval_current_ofs(1, 0),
2152 ctdb_every_second, ctdb);
2155 struct srvid_reply_handler_data {
2159 const char *srvid_str;
2162 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2167 struct srvid_reply_handler_data *d =
2168 (struct srvid_reply_handler_data *)private_data;
2172 if (data.dsize != sizeof(ret)) {
2173 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2177 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2178 ret = *(int32_t *)data.dptr;
2181 ("%s failed with result %d\n", d->srvid_str, ret));
2185 if (!d->wait_for_all) {
2190 /* Wait for all replies */
2192 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2193 if (d->nodes[i] == ret) {
2195 ("%s reply received from node %u\n",
2196 d->srvid_str, ret));
2199 if (d->nodes[i] != -1) {
2200 /* Found a node that hasn't yet replied */
2206 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2207 * or replies from all connected nodes. arg is the data argument to
2208 * pass in the srvid_request structure - pass 0 if this isn't needed.
2210 static int srvid_broadcast(struct ctdb_context *ctdb,
2211 uint64_t srvid, uint32_t *arg,
2212 const char *srvid_str, bool wait_for_all)
2217 uint64_t reply_srvid;
2218 struct srvid_request request;
2219 struct srvid_request_data request_data;
2220 struct srvid_reply_handler_data reply_data;
2223 ZERO_STRUCT(request);
2225 /* Time ticks to enable timeouts to be processed */
2226 event_add_timed(ctdb->ev, ctdb,
2227 timeval_current_ofs(1, 0),
2228 ctdb_every_second, ctdb);
2230 pnn = ctdb_get_pnn(ctdb);
2231 reply_srvid = getpid();
2235 request.srvid = reply_srvid;
2237 data.dptr = (uint8_t *)&request;
2238 data.dsize = sizeof(request);
2240 request_data.pnn = pnn;
2241 request_data.srvid = reply_srvid;
2242 request_data.data = *arg;
2244 data.dptr = (uint8_t *)&request_data;
2245 data.dsize = sizeof(request_data);
2248 /* Register message port for reply from recovery master */
2249 ctdb_client_set_message_handler(ctdb, reply_srvid,
2250 srvid_broadcast_reply_handler,
2253 reply_data.wait_for_all = wait_for_all;
2254 reply_data.nodes = NULL;
2255 reply_data.srvid_str = srvid_str;
2258 reply_data.done = false;
2261 struct ctdb_node_map *nodemap;
2263 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2264 CTDB_CURRENT_NODE, ctdb, &nodemap);
2267 ("Unable to get nodemap from current node, try again\n"));
2272 if (reply_data.nodes != NULL) {
2273 talloc_free(reply_data.nodes);
2275 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2278 talloc_free(nodemap);
2281 /* Send to all connected nodes. Only recmaster replies */
2282 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2285 /* This can only happen if the socket is closed and
2286 * there's no way to recover from that, so don't try
2290 ("Failed to send %s request to connected nodes\n",
2295 tv = timeval_current();
2296 /* This loop terminates the reply is received */
2297 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2298 event_loop_once(ctdb->ev);
2301 if (!reply_data.done) {
2303 ("Still waiting for confirmation of %s\n", srvid_str));
2308 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2310 talloc_free(reply_data.nodes);
2315 static int ipreallocate(struct ctdb_context *ctdb)
2317 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2318 "IP reallocation", false);
2322 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2324 return ipreallocate(ctdb);
2328 add a public ip address to a node
2330 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2333 int len, retries = 0;
2335 ctdb_sock_addr addr;
2336 struct ctdb_control_ip_iface *pub;
2337 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2338 struct ctdb_all_public_ips *ips;
2342 talloc_free(tmp_ctx);
2346 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2347 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2348 talloc_free(tmp_ctx);
2352 /* read the public ip list from the node */
2353 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2355 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2356 talloc_free(tmp_ctx);
2359 for (i=0;i<ips->num;i++) {
2360 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2361 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2368 /* Dont timeout. This command waits for an ip reallocation
2369 which sometimes can take wuite a while if there has
2370 been a recent recovery
2374 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2375 pub = talloc_size(tmp_ctx, len);
2376 CTDB_NO_MEMORY(ctdb, pub);
2380 pub->len = strlen(argv[1])+1;
2381 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2384 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2386 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2390 } while (retries < 5 && ret != 0);
2392 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2393 talloc_free(tmp_ctx);
2397 if (rebalance_node(ctdb, options.pnn) != 0) {
2398 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2402 talloc_free(tmp_ctx);
2407 add a public ip address to a node
2409 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2411 ctdb_sock_addr addr;
2417 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2418 printf("Badly formed ip : %s\n", argv[0]);
2422 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2427 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2429 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2431 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2432 struct ctdb_node_map *nodemap=NULL;
2433 struct ctdb_all_public_ips *ips;
2436 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2438 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2442 /* remove it from the nodes that are not hosting the ip currently */
2443 for(i=0;i<nodemap->num;i++){
2444 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2447 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2449 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2453 for (j=0;j<ips->num;j++) {
2454 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2462 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2466 options.pnn = nodemap->nodes[i].pnn;
2467 control_delip(ctdb, argc, argv);
2471 /* remove it from every node (also the one hosting it) */
2472 for(i=0;i<nodemap->num;i++){
2473 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2476 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2478 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2482 for (j=0;j<ips->num;j++) {
2483 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2491 options.pnn = nodemap->nodes[i].pnn;
2492 control_delip(ctdb, argc, argv);
2495 talloc_free(tmp_ctx);
2500 delete a public ip address from a node
2502 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2505 ctdb_sock_addr addr;
2506 struct ctdb_control_ip_iface pub;
2507 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2508 struct ctdb_all_public_ips *ips;
2511 talloc_free(tmp_ctx);
2515 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2516 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2520 if (options.pnn == CTDB_BROADCAST_ALL) {
2521 return control_delip_all(ctdb, argc, argv, &addr);
2528 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2530 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2531 talloc_free(tmp_ctx);
2535 for (i=0;i<ips->num;i++) {
2536 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2542 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2543 ctdb_addr_to_str(&addr)));
2544 talloc_free(tmp_ctx);
2548 /* This is an optimisation. If this node is hosting the IP
2549 * then try to move it somewhere else without invoking a full
2550 * takeover run. We don't care if this doesn't work!
2552 if (ips->ips[i].pnn == options.pnn) {
2553 (void) try_moveip(ctdb, &addr, -1);
2556 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2558 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2559 talloc_free(tmp_ctx);
2563 talloc_free(tmp_ctx);
2567 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2568 int argc, const char **argv)
2570 struct ctdb_control_killtcp *killtcp;
2571 int max_entries, current, i;
2572 struct timeval timeout;
2573 char line[128], src[128], dst[128];
2576 struct client_async_data *async_data;
2577 struct ctdb_client_control_state *state;
2587 while (!feof(stdin)) {
2588 if (fgets(line, sizeof(line), stdin) == NULL) {
2592 /* Silently skip empty lines */
2593 if (line[0] == '\n') {
2597 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2598 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2600 talloc_free(killtcp);
2604 if (current >= max_entries) {
2605 max_entries += 1024;
2606 killtcp = talloc_realloc(ctdb, killtcp,
2607 struct ctdb_control_killtcp,
2609 CTDB_NO_MEMORY(ctdb, killtcp);
2612 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2613 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2615 talloc_free(killtcp);
2619 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2620 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2622 talloc_free(killtcp);
2629 async_data = talloc_zero(ctdb, struct client_async_data);
2630 if (async_data == NULL) {
2631 talloc_free(killtcp);
2635 for (i = 0; i < current; i++) {
2637 data.dsize = sizeof(struct ctdb_control_killtcp);
2638 data.dptr = (unsigned char *)&killtcp[i];
2640 timeout = TIMELIMIT();
2641 state = ctdb_control_send(ctdb, options.pnn, 0,
2642 CTDB_CONTROL_KILL_TCP, 0, data,
2643 async_data, &timeout, NULL);
2645 if (state == NULL) {
2647 ("Failed to call async killtcp control to node %u\n",
2649 talloc_free(killtcp);
2653 ctdb_client_async_add(async_data, state);
2656 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2657 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2658 talloc_free(killtcp);
2662 talloc_free(killtcp);
2668 kill a tcp connection
2670 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2673 struct ctdb_control_killtcp killtcp;
2675 assert_single_node_only();
2678 return kill_tcp_from_file(ctdb, argc, argv);
2685 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2686 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2690 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2691 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2695 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2697 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2708 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2711 ctdb_sock_addr addr;
2713 assert_single_node_only();
2719 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2720 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2724 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2726 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2734 register a server id
2736 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2739 struct ctdb_server_id server_id;
2745 server_id.pnn = strtoul(argv[0], NULL, 0);
2746 server_id.type = strtoul(argv[1], NULL, 0);
2747 server_id.server_id = strtoul(argv[2], NULL, 0);
2749 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2751 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2754 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2760 unregister a server id
2762 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2765 struct ctdb_server_id server_id;
2771 server_id.pnn = strtoul(argv[0], NULL, 0);
2772 server_id.type = strtoul(argv[1], NULL, 0);
2773 server_id.server_id = strtoul(argv[2], NULL, 0);
2775 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2777 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2784 check if a server id exists
2786 static int chksrvid(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_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2802 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2807 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2809 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2815 get a list of all server ids that are registered on a node
2817 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2820 struct ctdb_server_id_list *server_ids;
2822 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2824 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2828 for (i=0; i<server_ids->num; i++) {
2829 printf("Server id %d:%d:%d\n",
2830 server_ids->server_ids[i].pnn,
2831 server_ids->server_ids[i].type,
2832 server_ids->server_ids[i].server_id);
2839 check if a server id exists
2841 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2843 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2849 talloc_free(tmp_ctx);
2853 ids = talloc_array(tmp_ctx, uint64_t, argc);
2854 result = talloc_array(tmp_ctx, uint8_t, argc);
2856 for (i = 0; i < argc; i++) {
2857 ids[i] = strtoull(argv[i], NULL, 0);
2860 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2861 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2863 talloc_free(tmp_ctx);
2867 for (i=0; i < argc; i++) {
2868 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2869 result[i] ? "exists" : "does not exist");
2872 talloc_free(tmp_ctx);
2877 send a tcp tickle ack
2879 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2882 ctdb_sock_addr src, dst;
2888 if (!parse_ip_port(argv[0], &src)) {
2889 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2893 if (!parse_ip_port(argv[1], &dst)) {
2894 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2898 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2902 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2909 display public ip status
2911 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2914 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2915 struct ctdb_all_public_ips *ips;
2917 if (options.pnn == CTDB_BROADCAST_ALL) {
2918 /* read the list of public ips from all nodes */
2919 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2921 /* read the public ip list from this node */
2922 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2925 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2926 talloc_free(tmp_ctx);
2930 if (options.machinereadable){
2931 printm(":Public IP:Node:");
2932 if (options.verbose){
2933 printm("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2937 if (options.pnn == CTDB_BROADCAST_ALL) {
2938 printf("Public IPs on ALL nodes\n");
2940 printf("Public IPs on node %u\n", options.pnn);
2944 for (i=1;i<=ips->num;i++) {
2945 struct ctdb_control_public_ip_info *info = NULL;
2947 char *aciface = NULL;
2948 char *avifaces = NULL;
2949 char *cifaces = NULL;
2951 if (options.pnn == CTDB_BROADCAST_ALL) {
2952 pnn = ips->ips[ips->num-i].pnn;
2958 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2959 &ips->ips[ips->num-i].addr, &info);
2966 for (j=0; j < info->num; j++) {
2967 if (cifaces == NULL) {
2968 cifaces = talloc_strdup(info,
2969 info->ifaces[j].name);
2971 cifaces = talloc_asprintf_append(cifaces,
2973 info->ifaces[j].name);
2976 if (info->active_idx == j) {
2977 aciface = info->ifaces[j].name;
2980 if (info->ifaces[j].link_state == 0) {
2984 if (avifaces == NULL) {
2985 avifaces = talloc_strdup(info, info->ifaces[j].name);
2987 avifaces = talloc_asprintf_append(avifaces,
2989 info->ifaces[j].name);
2994 if (options.machinereadable){
2996 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2997 ips->ips[ips->num-i].pnn);
2998 if (options.verbose){
3001 avifaces?avifaces:"",
3002 cifaces?cifaces:"");
3006 if (options.verbose) {
3007 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3008 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3009 ips->ips[ips->num-i].pnn,
3011 avifaces?avifaces:"",
3012 cifaces?cifaces:"");
3015 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3016 ips->ips[ips->num-i].pnn);
3022 talloc_free(tmp_ctx);
3029 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3032 ctdb_sock_addr addr;
3033 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3034 struct ctdb_control_public_ip_info *info;
3037 talloc_free(tmp_ctx);
3041 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3042 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3046 /* read the public ip info from this node */
3047 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3048 tmp_ctx, &addr, &info);
3050 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3051 argv[0], options.pnn));
3052 talloc_free(tmp_ctx);
3056 printf("Public IP[%s] info on node %u\n",
3057 ctdb_addr_to_str(&info->ip.addr),
3060 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3061 ctdb_addr_to_str(&info->ip.addr),
3062 info->ip.pnn, info->num);
3064 for (i=0; i<info->num; i++) {
3065 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3067 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3068 i+1, info->ifaces[i].name,
3069 info->ifaces[i].link_state?"up":"down",
3070 (unsigned int)info->ifaces[i].references,
3071 (i==info->active_idx)?" (active)":"");
3074 talloc_free(tmp_ctx);
3079 display interfaces status
3081 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3083 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3085 struct ctdb_control_get_ifaces *ifaces;
3088 /* read the public ip list from this node */
3089 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3091 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3093 talloc_free(tmp_ctx);
3097 if (options.machinereadable){
3098 printm(":Name:LinkStatus:References:\n");
3100 printf("Interfaces on node %u\n", options.pnn);
3103 for (i=0; i<ifaces->num; i++) {
3104 if (options.machinereadable){
3105 printm(":%s:%s:%u:\n",
3106 ifaces->ifaces[i].name,
3107 ifaces->ifaces[i].link_state?"1":"0",
3108 (unsigned int)ifaces->ifaces[i].references);
3110 printf("name:%s link:%s references:%u\n",
3111 ifaces->ifaces[i].name,
3112 ifaces->ifaces[i].link_state?"up":"down",
3113 (unsigned int)ifaces->ifaces[i].references);
3117 talloc_free(tmp_ctx);
3123 set link status of an interface
3125 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3128 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3129 struct ctdb_control_iface_info info;
3137 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3138 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3140 talloc_free(tmp_ctx);
3143 strcpy(info.name, argv[0]);
3145 if (strcmp(argv[1], "up") == 0) {
3146 info.link_state = 1;
3147 } else if (strcmp(argv[1], "down") == 0) {
3148 info.link_state = 0;
3150 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3152 talloc_free(tmp_ctx);
3156 /* read the public ip list from this node */
3157 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3160 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3161 argv[0], options.pnn));
3162 talloc_free(tmp_ctx);
3166 talloc_free(tmp_ctx);
3171 display pid of a ctdb daemon
3173 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3178 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3180 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3183 printf("Pid:%d\n", pid);
3188 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3190 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3192 update_flags_handler_t handler,
3197 struct ctdb_node_map *nodemap = NULL;
3201 /* Check if the node is already in the desired state */
3202 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3204 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3207 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3208 if (set_flag == flag_is_set) {
3209 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3210 (set_flag ? "already" : "not"), desc));
3215 if (!handler(ctdb, data)) {
3216 DEBUG(DEBUG_WARNING,
3217 ("Failed to send control to set state %s on node %u, try again\n",
3218 desc, options.pnn));
3223 /* Read the nodemap and verify the change took effect.
3224 * Even if the above control/hanlder timed out then it
3225 * could still have worked!
3227 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3230 DEBUG(DEBUG_WARNING,
3231 ("Unable to get nodemap from local node, try again\n"));
3233 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3234 } while (nodemap == NULL || (set_flag != flag_is_set));
3236 return ipreallocate(ctdb);
3239 /* Administratively disable a node */
3240 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3244 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3245 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3249 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3251 return update_flags_and_ipreallocate(ctdb, NULL,
3252 update_flags_disabled,
3253 NODE_FLAGS_PERMANENTLY_DISABLED,
3255 true /* set_flag*/);
3258 /* Administratively re-enable a node */
3259 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3263 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3264 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3268 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3270 return update_flags_and_ipreallocate(ctdb, NULL,
3271 update_flags_not_disabled,
3272 NODE_FLAGS_PERMANENTLY_DISABLED,
3274 false /* set_flag*/);
3278 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3282 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3287 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3289 return update_flags_and_ipreallocate(ctdb, NULL,
3290 update_flags_stopped,
3293 true /* set_flag*/);
3296 /* Continue a stopped node */
3297 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3301 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3306 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3308 return update_flags_and_ipreallocate(ctdb, NULL,
3309 update_flags_not_stopped,
3312 false /* set_flag */);
3315 static uint32_t get_generation(struct ctdb_context *ctdb)
3317 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3318 struct ctdb_vnn_map *vnnmap=NULL;
3320 uint32_t generation;
3322 /* wait until the recmaster is not in recovery mode */
3324 uint32_t recmode, recmaster;
3326 if (vnnmap != NULL) {
3327 talloc_free(vnnmap);
3331 /* get the recmaster */
3332 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3334 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3335 talloc_free(tmp_ctx);
3339 /* get recovery mode */
3340 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3342 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3343 talloc_free(tmp_ctx);
3347 /* get the current generation number */
3348 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3350 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3351 talloc_free(tmp_ctx);
3355 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3356 generation = vnnmap->generation;
3357 talloc_free(tmp_ctx);
3365 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3367 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3370 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3375 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3377 struct ctdb_ban_time bantime;
3383 bantime.pnn = options.pnn;
3384 bantime.time = strtoul(argv[0], NULL, 0);
3386 if (bantime.time == 0) {
3387 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3391 return update_flags_and_ipreallocate(ctdb, &bantime,
3392 update_state_banned,
3395 true /* set_flag*/);
3400 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3402 struct ctdb_ban_time bantime;
3404 bantime.pnn = options.pnn;
3407 return update_flags_and_ipreallocate(ctdb, &bantime,
3408 update_state_banned,
3411 false /* set_flag*/);
3415 show ban information for a node
3417 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3420 struct ctdb_node_map *nodemap=NULL;
3421 struct ctdb_ban_time *bantime;
3423 /* verify the node exists */
3424 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3426 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3430 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3432 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3436 if (bantime->time == 0) {
3437 printf("Node %u is not banned\n", bantime->pnn);
3439 printf("Node %u is banned, %d seconds remaining\n",
3440 bantime->pnn, bantime->time);
3449 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3453 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3455 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3465 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3468 uint32_t generation, next_generation;
3470 /* record the current generation number */
3471 generation = get_generation(ctdb);
3473 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3475 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3479 /* wait until we are in a new generation */
3481 next_generation = get_generation(ctdb);
3482 if (next_generation != generation) {
3493 display monitoring mode of a remote node
3495 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3500 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3502 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3505 if (!options.machinereadable){
3506 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3509 printm(":%d:\n",monmode);
3516 display capabilities of a remote node
3518 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3520 uint32_t capabilities;
3523 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3525 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3529 if (!options.machinereadable){
3530 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3531 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3532 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3533 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3535 printm(":RECMASTER:LMASTER:LVS:NATGW:\n");
3536 printm(":%d:%d:%d:%d:\n",
3537 !!(capabilities&CTDB_CAP_RECMASTER),
3538 !!(capabilities&CTDB_CAP_LMASTER),
3539 !!(capabilities&CTDB_CAP_LVS),
3540 !!(capabilities&CTDB_CAP_NATGW));
3546 display lvs configuration
3549 static uint32_t lvs_exclude_flags[] = {
3550 /* Look for a nice healthy node */
3551 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3552 /* If not found, an UNHEALTHY node will do */
3553 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3557 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3559 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3560 struct ctdb_node_map *orig_nodemap=NULL;
3561 struct ctdb_node_map *nodemap;
3564 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3565 tmp_ctx, &orig_nodemap);
3567 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3568 talloc_free(tmp_ctx);
3572 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3573 CTDB_CAP_LVS, false);
3574 if (nodemap == NULL) {
3582 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3583 struct ctdb_node_map *t =
3584 filter_nodemap_by_flags(ctdb, nodemap,
3585 lvs_exclude_flags[i]);
3592 /* At least 1 node without excluded flags */
3594 for (j = 0; j < t->num; j++) {
3595 printf("%d:%s\n", t->nodes[j].pnn,
3596 ctdb_addr_to_str(&t->nodes[j].addr));
3603 talloc_free(tmp_ctx);
3608 display who is the lvs master
3610 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3612 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3613 struct ctdb_node_map *nodemap=NULL;
3616 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3619 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3620 talloc_free(tmp_ctx);
3624 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3625 struct ctdb_node_map *t =
3626 filter_nodemap_by_flags(ctdb, nodemap,
3627 lvs_exclude_flags[i]);
3634 struct ctdb_node_map *n;
3635 n = filter_nodemap_by_capabilities(ctdb,
3646 if (options.machinereadable) {
3647 printm("%d\n", n->nodes[0].pnn);
3649 printf("Node %d is LVS master\n", n->nodes[0].pnn);
3657 printf("There is no LVS master\n");
3660 talloc_free(tmp_ctx);
3665 disable monitoring on a node
3667 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3672 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3674 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3677 printf("Monitoring mode:%s\n","DISABLED");
3683 enable monitoring on a node
3685 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3690 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3692 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3695 printf("Monitoring mode:%s\n","ACTIVE");
3701 display remote list of keys/data for a db
3703 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3705 const char *db_name;
3706 struct ctdb_db_context *ctdb_db;
3708 struct ctdb_dump_db_context c;
3715 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3719 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3720 if (ctdb_db == NULL) {
3721 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3725 if (options.printlmaster) {
3726 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3727 ctdb, &ctdb->vnn_map);
3729 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3737 c.printemptyrecords = (bool)options.printemptyrecords;
3738 c.printdatasize = (bool)options.printdatasize;
3739 c.printlmaster = (bool)options.printlmaster;
3740 c.printhash = (bool)options.printhash;
3741 c.printrecordflags = (bool)options.printrecordflags;
3743 /* traverse and dump the cluster tdb */
3744 ret = ctdb_dump_db(ctdb_db, &c);
3746 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3747 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3748 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3752 talloc_free(ctdb_db);
3754 printf("Dumped %d records\n", ret);
3758 struct cattdb_data {
3759 struct ctdb_context *ctdb;
3763 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3765 struct cattdb_data *d = private_data;
3766 struct ctdb_dump_db_context c;
3772 c.printemptyrecords = (bool)options.printemptyrecords;
3773 c.printdatasize = (bool)options.printdatasize;
3774 c.printlmaster = false;
3775 c.printhash = (bool)options.printhash;
3776 c.printrecordflags = true;
3778 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3782 cat the local tdb database using same format as catdb
3784 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3786 const char *db_name;
3787 struct ctdb_db_context *ctdb_db;
3788 struct cattdb_data d;
3795 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3799 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3800 if (ctdb_db == NULL) {
3801 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3805 /* traverse the local tdb */
3808 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3809 printf("Failed to cattdb data\n");
3812 talloc_free(ctdb_db);
3814 printf("Dumped %d records\n", d.count);
3819 display the content of a database key
3821 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3823 const char *db_name;
3824 struct ctdb_db_context *ctdb_db;
3825 struct ctdb_record_handle *h;
3826 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3834 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3838 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3839 if (ctdb_db == NULL) {
3840 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3844 key.dptr = discard_const(argv[1]);
3845 key.dsize = strlen((char *)key.dptr);
3847 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3849 printf("Failed to fetch record '%s' on node %d\n",
3850 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3851 talloc_free(tmp_ctx);
3855 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3857 talloc_free(tmp_ctx);
3858 talloc_free(ctdb_db);
3863 display the content of a database key
3865 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3867 const char *db_name;
3868 struct ctdb_db_context *ctdb_db;
3869 struct ctdb_record_handle *h;
3870 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3878 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3882 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3883 if (ctdb_db == NULL) {
3884 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3888 key.dptr = discard_const(argv[1]);
3889 key.dsize = strlen((char *)key.dptr);
3891 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3893 printf("Failed to fetch record '%s' on node %d\n",
3894 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3895 talloc_free(tmp_ctx);
3899 data.dptr = discard_const(argv[2]);
3900 data.dsize = strlen((char *)data.dptr);
3902 if (ctdb_record_store(h, data) != 0) {
3903 printf("Failed to store record\n");
3907 talloc_free(tmp_ctx);
3908 talloc_free(ctdb_db);
3913 fetch a record from a persistent database
3915 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3917 const char *db_name;
3918 struct ctdb_db_context *ctdb_db;
3919 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3920 struct ctdb_transaction_handle *h;
3927 talloc_free(tmp_ctx);
3931 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3932 talloc_free(tmp_ctx);
3936 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3938 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3939 talloc_free(tmp_ctx);
3943 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3944 if (ctdb_db == NULL) {
3945 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3946 talloc_free(tmp_ctx);
3950 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3952 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3953 talloc_free(tmp_ctx);
3957 key.dptr = discard_const(argv[1]);
3958 key.dsize = strlen(argv[1]);
3959 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3961 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3962 talloc_free(tmp_ctx);
3966 if (data.dsize == 0 || data.dptr == NULL) {
3967 DEBUG(DEBUG_ERR,("Record is empty\n"));
3968 talloc_free(tmp_ctx);
3973 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3975 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3976 talloc_free(tmp_ctx);
3979 sys_write(fd, data.dptr, data.dsize);
3982 sys_write(1, data.dptr, data.dsize);
3985 /* abort the transaction */
3989 talloc_free(tmp_ctx);
3994 fetch a record from a tdb-file
3996 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3998 const char *tdb_file;
4001 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4010 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4012 printf("Failed to open TDB file %s\n", tdb_file);
4016 if (!strncmp(argv[1], "0x", 2)) {
4017 key = hextodata(tmp_ctx, argv[1] + 2);
4018 if (key.dsize == 0) {
4019 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4023 key.dptr = discard_const(argv[1]);
4024 key.dsize = strlen(argv[1]);
4027 data = tdb_fetch(tdb, key);
4028 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4029 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4037 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4039 printf("Failed to open output file %s\n", argv[2]);
4042 if (options.verbose){
4043 sys_write(fd, data.dptr, data.dsize);
4045 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4049 if (options.verbose){
4050 sys_write(1, data.dptr, data.dsize);
4052 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4056 talloc_free(tmp_ctx);
4061 store a record and header to a tdb-file
4063 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4065 const char *tdb_file;
4067 TDB_DATA key, value, data;
4068 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4069 struct ctdb_ltdb_header header;
4077 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4079 printf("Failed to open TDB file %s\n", tdb_file);
4083 if (!strncmp(argv[1], "0x", 2)) {
4084 key = hextodata(tmp_ctx, argv[1] + 2);
4085 if (key.dsize == 0) {
4086 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4090 key.dptr = discard_const(argv[1]);
4091 key.dsize = strlen(argv[1]);
4094 if (!strncmp(argv[2], "0x", 2)) {
4095 value = hextodata(tmp_ctx, argv[2] + 2);
4096 if (value.dsize == 0) {
4097 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4101 value.dptr = discard_const(argv[2]);
4102 value.dsize = strlen(argv[2]);
4105 ZERO_STRUCT(header);
4107 header.rsn = atoll(argv[3]);
4110 header.dmaster = atoi(argv[4]);
4113 header.flags = atoi(argv[5]);
4116 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4117 data.dptr = talloc_size(tmp_ctx, data.dsize);
4118 if (data.dptr == NULL) {
4119 printf("Failed to allocate header+value\n");
4123 *(struct ctdb_ltdb_header *)data.dptr = header;
4124 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4126 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4127 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4134 talloc_free(tmp_ctx);
4139 write a record to a persistent database
4141 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4143 const char *db_name;
4144 struct ctdb_db_context *ctdb_db;
4145 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4146 struct ctdb_transaction_handle *h;
4152 talloc_free(tmp_ctx);
4156 fd = open(argv[2], O_RDONLY);
4158 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4159 talloc_free(tmp_ctx);
4163 ret = fstat(fd, &st);
4165 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4167 talloc_free(tmp_ctx);
4171 if (!S_ISREG(st.st_mode)) {
4172 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4174 talloc_free(tmp_ctx);
4178 data.dsize = st.st_size;
4179 if (data.dsize == 0) {
4182 data.dptr = talloc_size(tmp_ctx, data.dsize);
4183 if (data.dptr == NULL) {
4184 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4186 talloc_free(tmp_ctx);
4189 ret = sys_read(fd, data.dptr, data.dsize);
4190 if (ret != data.dsize) {
4191 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4193 talloc_free(tmp_ctx);
4202 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4203 if (ctdb_db == NULL) {
4204 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4205 talloc_free(tmp_ctx);
4209 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4211 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4212 talloc_free(tmp_ctx);
4216 key.dptr = discard_const(argv[1]);
4217 key.dsize = strlen(argv[1]);
4218 ret = ctdb_transaction_store(h, key, data);
4220 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4221 talloc_free(tmp_ctx);
4225 ret = ctdb_transaction_commit(h);
4227 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4228 talloc_free(tmp_ctx);
4233 talloc_free(tmp_ctx);
4238 * delete a record from a persistent database
4240 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4242 const char *db_name;
4243 struct ctdb_db_context *ctdb_db;
4244 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4245 struct ctdb_transaction_handle *h;
4252 talloc_free(tmp_ctx);
4256 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4257 talloc_free(tmp_ctx);
4261 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4263 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4264 talloc_free(tmp_ctx);
4268 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4269 if (ctdb_db == NULL) {
4270 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4271 talloc_free(tmp_ctx);
4275 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4277 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4278 talloc_free(tmp_ctx);
4282 key.dptr = discard_const(argv[1]);
4283 key.dsize = strlen(argv[1]);
4284 ret = ctdb_transaction_store(h, key, tdb_null);
4286 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4287 talloc_free(tmp_ctx);
4291 ret = ctdb_transaction_commit(h);
4293 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4294 talloc_free(tmp_ctx);
4298 talloc_free(tmp_ctx);
4302 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4307 const char *ret; /* Next byte after successfully parsed value */
4309 /* Error, unless someone says otherwise */
4311 /* Indicates no value to parse */
4314 /* Skip whitespace */
4315 n = strspn(s, " \t");
4319 /* Quoted ASCII string - no wide characters! */
4321 n = strcspn(t, "\"");
4325 data->dptr = talloc_memdup(mem_ctx, t, n);
4326 CTDB_NOMEM_ABORT(data->dptr);
4330 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4333 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4339 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4340 TDB_DATA *key, TDB_DATA *value)
4342 char line [1024]; /* FIXME: make this more flexible? */
4346 ptr = fgets(line, sizeof(line), file);
4353 t = ptrans_parse_string(mem_ctx, line, key);
4354 if (t == NULL || key->dptr == NULL) {
4355 /* Line Ignored but not EOF */
4360 t = ptrans_parse_string(mem_ctx, t, value);
4362 /* Line Ignored but not EOF */
4363 talloc_free(key->dptr);
4372 * Update a persistent database as per file/stdin
4374 static int control_ptrans(struct ctdb_context *ctdb,
4375 int argc, const char **argv)
4377 const char *db_name;
4378 struct ctdb_db_context *ctdb_db;
4379 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4380 struct ctdb_transaction_handle *h;
4381 TDB_DATA key, value;
4386 talloc_free(tmp_ctx);
4392 file = fopen(argv[1], "r");
4394 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4395 talloc_free(tmp_ctx);
4402 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4403 if (ctdb_db == NULL) {
4404 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4408 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4410 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4414 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4415 if (key.dsize != 0) {
4416 ret = ctdb_transaction_store(h, key, value);
4417 /* Minimise memory use */
4418 talloc_free(key.dptr);
4419 if (value.dptr != NULL) {
4420 talloc_free(value.dptr);
4423 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4424 ctdb_transaction_cancel(h);
4430 ret = ctdb_transaction_commit(h);
4432 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4436 if (file != stdin) {
4439 talloc_free(tmp_ctx);
4443 if (file != stdin) {
4447 talloc_free(tmp_ctx);
4452 check if a service is bound to a port or not
4454 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4459 struct sockaddr_in sin;
4462 printf("Use: ctdb chktcport <port>\n");
4466 port = atoi(argv[0]);
4468 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4470 printf("Failed to open local socket\n");
4474 v = fcntl(s, F_GETFL, 0);
4475 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4476 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4479 bzero(&sin, sizeof(sin));
4480 sin.sin_family = PF_INET;
4481 sin.sin_port = htons(port);
4482 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4485 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4493 /* Reload public IPs on a specified nodes */
4494 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4496 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4502 assert_single_node_only();
4508 /* Determine the nodes where IPs need to be reloaded */
4509 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4510 options.pnn, true, &nodes, &pnn_mode)) {
4516 /* Disable takeover runs on all connected nodes. A reply
4517 * indicating success is needed from each node so all nodes
4518 * will need to be active. This will retry until maxruntime
4519 * is exceeded, hence no error handling.
4521 * A check could be added to not allow reloading of IPs when
4522 * there are disconnected nodes. However, this should
4523 * probably be left up to the administrator.
4525 timeout = LONGTIMEOUT;
4526 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4527 "Disable takeover runs", true);
4529 /* Now tell all the desired nodes to reload their public IPs.
4530 * Keep trying this until it succeeds. This assumes all
4531 * failures are transient, which might not be true...
4533 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4534 nodes, 0, LONGTIMELIMIT(),
4536 NULL, NULL, NULL) != 0) {
4538 ("Unable to reload IPs on some nodes, try again.\n"));
4542 /* It isn't strictly necessary to wait until takeover runs are
4543 * re-enabled but doing so can't hurt.
4546 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4547 "Enable takeover runs", true);
4553 talloc_free(tmp_ctx);
4558 display a list of the databases on a remote ctdb
4560 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4563 struct ctdb_dbid_map *dbmap=NULL;
4565 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4567 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4571 if(options.machinereadable){
4572 printm(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4573 for(i=0;i<dbmap->num;i++){
4581 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4582 dbmap->dbs[i].dbid, ctdb, &path);
4583 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4584 dbmap->dbs[i].dbid, ctdb, &name);
4585 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4586 dbmap->dbs[i].dbid, ctdb, &health);
4587 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4588 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4589 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4590 printm(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4591 dbmap->dbs[i].dbid, name, path,
4592 !!(persistent), !!(sticky),
4593 !!(health), !!(readonly));
4598 printf("Number of databases:%d\n", dbmap->num);
4599 for(i=0;i<dbmap->num;i++){
4607 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4608 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4609 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4610 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4611 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4612 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4613 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4614 dbmap->dbs[i].dbid, name, path,
4615 persistent?" PERSISTENT":"",
4616 sticky?" STICKY":"",
4617 readonly?" READONLY":"",
4618 health?" UNHEALTHY":"");
4625 display the status of a database on a remote ctdb
4627 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4629 const char *db_name;
4639 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4643 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4644 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4645 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4646 db_id, db_name, path,
4647 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4648 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4649 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4650 (health ? health : "OK"));
4656 check if the local node is recmaster or not
4657 it will return 1 if this node is the recmaster and 0 if it is not
4658 or if the local ctdb daemon could not be contacted
4660 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4662 uint32_t mypnn, recmaster;
4665 assert_single_node_only();
4667 mypnn = getpnn(ctdb);
4669 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4671 printf("Failed to get the recmaster\n");
4675 if (recmaster != mypnn) {
4676 printf("this node is not the recmaster\n");
4680 printf("this node is the recmaster\n");
4687 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4690 struct timeval tv = timeval_current();
4691 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4693 printf("Unable to get ping response from node %u\n", options.pnn);
4696 printf("response from %u time=%.6f sec (%d clients)\n",
4697 options.pnn, timeval_elapsed(&tv), ret);
4704 get a node's runstate
4706 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4709 enum ctdb_runstate runstate;
4711 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4713 printf("Unable to get runstate response from node %u\n",
4718 enum ctdb_runstate t;
4720 for (i=0; i<argc; i++) {
4722 t = runstate_from_string(argv[i]);
4723 if (t == CTDB_RUNSTATE_UNKNOWN) {
4724 printf("Invalid run state (%s)\n", argv[i]);
4728 if (t == runstate) {
4735 printf("CTDB not in required run state (got %s)\n",
4736 runstate_to_string((enum ctdb_runstate)runstate));
4741 printf("%s\n", runstate_to_string(runstate));
4749 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4760 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4762 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4766 printf("%-23s = %u\n", name, value);
4773 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4784 value = strtoul(argv[1], NULL, 0);
4786 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4788 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4792 DEBUG(DEBUG_WARNING,
4793 ("Setting obsolete tunable variable '%s'\n",
4802 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4808 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4810 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4814 for (i=0;i<count;i++) {
4815 control_getvar(ctdb, 1, &list[i]);
4824 display debug level on a node
4826 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4831 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4833 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4836 const char *desc = get_debug_by_level(level);
4838 /* This should never happen */
4841 if (options.machinereadable){
4842 printm(":Name:Level:\n");
4843 printm(":%s:%d:\n", desc, level);
4845 printf("Node %u is at debug level %s (%d)\n",
4846 options.pnn, desc, level);
4853 display reclock file of a node
4855 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4858 const char *reclock;
4860 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4862 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4865 if (options.machinereadable){
4866 if (reclock != NULL) {
4867 printm("%s", reclock);
4870 if (reclock == NULL) {
4871 printf("No reclock file used.\n");
4873 printf("Reclock file:%s\n", reclock);
4881 set the reclock file of a node
4883 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4886 const char *reclock;
4890 } else if (argc == 1) {
4896 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4898 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4905 set the natgw state on/off
4907 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4910 uint32_t natgwstate;
4916 if (!strcmp(argv[0], "on")) {
4918 } else if (!strcmp(argv[0], "off")) {
4924 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4926 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4934 set the lmaster role on/off
4936 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4939 uint32_t lmasterrole;
4945 if (!strcmp(argv[0], "on")) {
4947 } else if (!strcmp(argv[0], "off")) {
4953 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4955 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4963 set the recmaster role on/off
4965 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4968 uint32_t recmasterrole;
4974 if (!strcmp(argv[0], "on")) {
4976 } else if (!strcmp(argv[0], "off")) {
4982 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4984 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4992 set debug level on a node or all nodes
4994 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5000 printf("You must specify the debug level. Valid levels are:\n");
5001 print_debug_levels(stdout);
5005 if (!parse_debug(argv[0], &level)) {
5006 printf("Invalid debug level, must be one of\n");
5007 print_debug_levels(stdout);
5011 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5013 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5022 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5028 priority = strtol(argv[0], NULL, 0);
5032 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5034 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5036 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5043 attach to a database
5045 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5047 const char *db_name;
5048 struct ctdb_db_context *ctdb_db;
5049 bool persistent = false;
5059 if (strcmp(argv[1], "persistent") != 0) {
5065 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5066 if (ctdb_db == NULL) {
5067 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5075 * detach from a database
5077 static int control_detach(struct ctdb_context *ctdb, int argc,
5082 int ret, i, status = 0;
5083 struct ctdb_node_map *nodemap = NULL;
5084 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5091 assert_single_node_only();
5093 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5096 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5097 "when recovery is active\n"));
5098 talloc_free(tmp_ctx);
5102 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5105 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5107 talloc_free(tmp_ctx);
5111 for (i=0; i<nodemap->num; i++) {
5114 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5118 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5122 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5123 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5124 "inactive (stopped or banned) node "
5125 "%u\n", nodemap->nodes[i].pnn));
5126 talloc_free(tmp_ctx);
5130 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5131 nodemap->nodes[i].pnn,
5132 "AllowClientDBAttach",
5135 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5136 "AllowClientDBAttach from node %u\n",
5137 nodemap->nodes[i].pnn));
5138 talloc_free(tmp_ctx);
5143 DEBUG(DEBUG_ERR, ("Database access is still active on "
5144 "node %u. Set AllowClientDBAttach=0 "
5146 nodemap->nodes[i].pnn));
5147 talloc_free(tmp_ctx);
5152 talloc_free(tmp_ctx);
5154 for (i=0; i<argc; i++) {
5155 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5159 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5160 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5161 "cannot be detached\n", argv[i]));
5166 ret = ctdb_detach(ctdb, db_id);
5168 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5180 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5182 struct ctdb_db_priority db_prio;
5189 db_prio.db_id = strtoul(argv[0], NULL, 0);
5190 db_prio.priority = strtoul(argv[1], NULL, 0);
5192 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5194 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5204 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5206 uint32_t db_id, priority;
5213 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5217 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5219 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5223 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5229 set the sticky records capability for a database
5231 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5233 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5241 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5245 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5247 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5248 talloc_free(tmp_ctx);
5252 talloc_free(tmp_ctx);
5257 set the readonly capability for a database
5259 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5261 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5269 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5273 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5275 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5276 talloc_free(tmp_ctx);
5280 talloc_free(tmp_ctx);
5287 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5297 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5301 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5303 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5307 printf("Sequence number:%lld\n", (long long)seqnum);
5313 run an eventscript on a node
5315 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5321 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5324 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5328 data.dptr = (unsigned char *)discard_const(argv[0]);
5329 data.dsize = strlen((char *)data.dptr) + 1;
5331 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5333 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5334 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5335 if (ret != 0 || res != 0) {
5336 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5337 talloc_free(tmp_ctx);
5340 talloc_free(tmp_ctx);
5344 #define DB_VERSION 1
5345 #define MAX_DB_NAME 64
5346 struct db_file_header {
5347 unsigned long version;
5349 unsigned long persistent;
5351 const char name[MAX_DB_NAME];
5354 struct backup_data {
5355 struct ctdb_marshall_buffer *records;
5358 bool traverse_error;
5361 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5363 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5364 struct ctdb_rec_data *rec;
5366 /* add the record */
5367 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5369 bd->traverse_error = true;
5370 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5373 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5374 if (bd->records == NULL) {
5375 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5376 bd->traverse_error = true;
5379 bd->records->count++;
5380 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5381 bd->len += rec->length;
5389 * backup a database to a file
5391 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5393 const char *db_name;
5395 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5396 struct db_file_header dbhdr;
5397 struct ctdb_db_context *ctdb_db;
5398 struct backup_data *bd;
5401 const char *reason = NULL;
5405 assert_single_node_only();
5408 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5412 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5416 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5417 db_id, tmp_ctx, &reason);
5419 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5421 talloc_free(tmp_ctx);
5425 uint32_t allow_unhealthy = 0;
5427 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5428 "AllowUnhealthyDBRead",
5431 if (allow_unhealthy != 1) {
5432 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5435 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5437 talloc_free(tmp_ctx);
5441 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5443 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5444 "tunnable AllowUnhealthyDBRead = %u\n",
5448 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5449 if (ctdb_db == NULL) {
5450 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5451 talloc_free(tmp_ctx);
5456 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5458 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5459 talloc_free(tmp_ctx);
5464 bd = talloc_zero(tmp_ctx, struct backup_data);
5466 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5467 talloc_free(tmp_ctx);
5471 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5472 if (bd->records == NULL) {
5473 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5474 talloc_free(tmp_ctx);
5478 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5479 bd->records->db_id = ctdb_db->db_id;
5480 /* traverse the database collecting all records */
5481 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5482 bd->traverse_error) {
5483 DEBUG(DEBUG_ERR,("Traverse error\n"));
5484 talloc_free(tmp_ctx);
5488 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5491 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5493 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5494 talloc_free(tmp_ctx);
5499 dbhdr.version = DB_VERSION;
5500 dbhdr.timestamp = time(NULL);
5501 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5502 dbhdr.size = bd->len;
5503 if (strlen(argv[0]) >= MAX_DB_NAME) {
5504 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5507 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5508 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
5510 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5513 ret = sys_write(fh, bd->records, bd->len);
5515 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5524 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5528 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5530 talloc_free(tmp_ctx);
5535 * restore a database from a file
5537 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5540 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5543 struct db_file_header dbhdr;
5544 struct ctdb_db_context *ctdb_db;
5545 struct ctdb_node_map *nodemap=NULL;
5546 struct ctdb_vnn_map *vnnmap=NULL;
5548 struct ctdb_control_wipe_database w;
5550 uint32_t generation;
5555 assert_single_node_only();
5557 if (argc < 1 || argc > 2) {
5558 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5562 fh = open(argv[0], O_RDONLY);
5564 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5565 talloc_free(tmp_ctx);
5569 sys_read(fh, &dbhdr, sizeof(dbhdr));
5570 if (dbhdr.version != DB_VERSION) {
5571 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5573 talloc_free(tmp_ctx);
5577 dbname = discard_const(dbhdr.name);
5579 dbname = discard_const(argv[1]);
5582 outdata.dsize = dbhdr.size;
5583 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5584 if (outdata.dptr == NULL) {
5585 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5587 talloc_free(tmp_ctx);
5590 sys_read(fh, outdata.dptr, outdata.dsize);
5593 tm = localtime(&dbhdr.timestamp);
5594 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5595 printf("Restoring database '%s' from backup @ %s\n",
5599 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5600 if (ctdb_db == NULL) {
5601 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5602 talloc_free(tmp_ctx);
5606 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5608 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5609 talloc_free(tmp_ctx);
5614 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5616 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5617 talloc_free(tmp_ctx);
5621 /* freeze all nodes */
5622 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5623 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5624 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5630 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5631 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5632 talloc_free(tmp_ctx);
5637 generation = vnnmap->generation;
5638 data.dptr = (void *)&generation;
5639 data.dsize = sizeof(generation);
5641 /* start a cluster wide transaction */
5642 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5643 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5645 TIMELIMIT(), false, data,
5648 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5653 w.db_id = ctdb_db->db_id;
5654 w.transaction_id = generation;
5656 data.dptr = (void *)&w;
5657 data.dsize = sizeof(w);
5659 /* wipe all the remote databases. */
5660 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5661 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5663 TIMELIMIT(), false, data,
5666 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5667 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5668 talloc_free(tmp_ctx);
5672 /* push the database */
5673 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5674 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5676 TIMELIMIT(), false, outdata,
5679 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5680 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5681 talloc_free(tmp_ctx);
5685 data.dptr = (void *)&ctdb_db->db_id;
5686 data.dsize = sizeof(ctdb_db->db_id);
5688 /* mark the database as healthy */
5689 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5690 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5692 TIMELIMIT(), false, data,
5695 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5696 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5697 talloc_free(tmp_ctx);
5701 data.dptr = (void *)&generation;
5702 data.dsize = sizeof(generation);
5704 /* commit all the changes */
5705 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5707 TIMELIMIT(), false, data,
5710 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5711 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5712 talloc_free(tmp_ctx);
5717 /* thaw all nodes */
5718 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5719 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5725 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5726 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5727 talloc_free(tmp_ctx);
5732 talloc_free(tmp_ctx);
5737 * dump a database backup from a file
5739 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5741 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5743 struct db_file_header dbhdr;
5747 struct ctdb_rec_data *rec = NULL;
5748 struct ctdb_marshall_buffer *m;
5749 struct ctdb_dump_db_context c;
5751 assert_single_node_only();
5754 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5758 fh = open(argv[0], O_RDONLY);
5760 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5761 talloc_free(tmp_ctx);
5765 sys_read(fh, &dbhdr, sizeof(dbhdr));
5766 if (dbhdr.version != DB_VERSION) {
5767 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5769 talloc_free(tmp_ctx);
5773 outdata.dsize = dbhdr.size;
5774 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5775 if (outdata.dptr == NULL) {
5776 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5778 talloc_free(tmp_ctx);
5781 sys_read(fh, outdata.dptr, outdata.dsize);
5783 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5785 tm = localtime(&dbhdr.timestamp);
5786 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5787 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5788 dbhdr.name, m->db_id, tbuf);
5792 c.printemptyrecords = (bool)options.printemptyrecords;
5793 c.printdatasize = (bool)options.printdatasize;
5794 c.printlmaster = false;
5795 c.printhash = (bool)options.printhash;
5796 c.printrecordflags = (bool)options.printrecordflags;
5798 for (i=0; i < m->count; i++) {
5802 /* we do not want the header splitted, so we pass NULL*/
5803 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5806 ctdb_dumpdb_record(ctdb, key, data, &c);
5809 printf("Dumped %d records\n", i);
5810 talloc_free(tmp_ctx);
5815 * wipe a database from a file
5817 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5820 const char *db_name;
5822 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5824 struct ctdb_db_context *ctdb_db;
5825 struct ctdb_node_map *nodemap = NULL;
5826 struct ctdb_vnn_map *vnnmap = NULL;
5828 struct ctdb_control_wipe_database w;
5830 uint32_t generation;
5833 assert_single_node_only();
5836 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5840 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5844 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5845 if (ctdb_db == NULL) {
5846 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5848 talloc_free(tmp_ctx);
5852 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5855 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5857 talloc_free(tmp_ctx);
5861 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5864 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5866 talloc_free(tmp_ctx);
5870 /* freeze all nodes */
5871 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5872 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5873 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5880 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5881 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5882 CTDB_RECOVERY_ACTIVE);
5883 talloc_free(tmp_ctx);
5888 generation = vnnmap->generation;
5889 data.dptr = (void *)&generation;
5890 data.dsize = sizeof(generation);
5892 /* start a cluster wide transaction */
5893 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5894 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5896 TIMELIMIT(), false, data,
5900 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5901 "transactions.\n"));
5905 w.db_id = ctdb_db->db_id;
5906 w.transaction_id = generation;
5908 data.dptr = (void *)&w;
5909 data.dsize = sizeof(w);
5911 /* wipe all the remote databases. */
5912 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5913 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5915 TIMELIMIT(), false, data,
5918 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5919 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5920 talloc_free(tmp_ctx);
5924 data.dptr = (void *)&ctdb_db->db_id;
5925 data.dsize = sizeof(ctdb_db->db_id);
5927 /* mark the database as healthy */
5928 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5929 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5931 TIMELIMIT(), false, data,
5934 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5935 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5936 talloc_free(tmp_ctx);
5940 data.dptr = (void *)&generation;
5941 data.dsize = sizeof(generation);
5943 /* commit all the changes */
5944 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5946 TIMELIMIT(), false, data,
5949 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5950 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5951 talloc_free(tmp_ctx);
5955 /* thaw all nodes */
5956 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5957 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5963 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5964 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5965 talloc_free(tmp_ctx);
5969 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5971 talloc_free(tmp_ctx);
5978 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5984 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5985 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5986 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5987 if (ret != 0 || res != 0) {
5988 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5989 talloc_free(tmp_ctx);
5992 sys_write(1, data.dptr, data.dsize);
5993 talloc_free(tmp_ctx);
5998 handler for memory dumps
6000 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6001 TDB_DATA data, void *private_data)
6003 sys_write(1, data.dptr, data.dsize);
6008 dump memory usage on the recovery daemon
6010 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6014 struct srvid_request rd;
6016 rd.pnn = ctdb_get_pnn(ctdb);
6017 rd.srvid = getpid();
6019 /* register a message port for receiveing the reply so that we
6020 can receive the reply
6022 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6025 data.dptr = (uint8_t *)&rd;
6026 data.dsize = sizeof(rd);
6028 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6030 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6034 /* this loop will terminate when we have received the reply */
6036 event_loop_once(ctdb->ev);
6043 send a message to a srvid
6045 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6047 unsigned long srvid;
6055 srvid = strtoul(argv[0], NULL, 0);
6057 data.dptr = (uint8_t *)discard_const(argv[1]);
6058 data.dsize= strlen(argv[1]);
6060 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6062 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6070 handler for msglisten
6072 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6073 TDB_DATA data, void *private_data)
6077 printf("Message received: ");
6078 for (i=0;i<data.dsize;i++) {
6079 printf("%c", data.dptr[i]);
6085 listen for messages on a messageport
6087 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6093 /* register a message port and listen for messages
6095 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6096 printf("Listening for messages on srvid:%d\n", (int)srvid);
6099 event_loop_once(ctdb->ev);
6106 list all nodes in the cluster
6107 we parse the nodes file directly
6109 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6111 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6112 struct ctdb_node_map *node_map;
6115 assert_single_node_only();
6117 node_map = read_nodes_file(mem_ctx);
6118 if (node_map == NULL) {
6119 talloc_free(mem_ctx);
6123 for (i = 0; i < node_map->num; i++) {
6126 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
6129 addr = ctdb_addr_to_str(&node_map->nodes[i].addr);
6130 if (options.machinereadable){
6131 printm(":%d:%s:\n", node_map->nodes[i].pnn, addr);
6133 printf("%s\n", addr);
6136 talloc_free(mem_ctx);
6142 reload the nodes file on the local node
6144 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6148 struct ctdb_node_map *nodemap=NULL;
6150 assert_single_node_only();
6152 mypnn = ctdb_get_pnn(ctdb);
6154 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6156 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6160 /* reload the nodes file on all remote nodes */
6161 for (i=0;i<nodemap->num;i++) {
6162 if (nodemap->nodes[i].pnn == mypnn) {
6165 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6166 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6167 nodemap->nodes[i].pnn);
6169 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6173 /* reload the nodes file on the local node */
6174 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6175 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6177 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6180 /* initiate a recovery */
6181 control_recover(ctdb, argc, argv);
6187 static const struct {
6189 int (*fn)(struct ctdb_context *, int, const char **);
6191 bool without_daemon; /* can be run without daemon running ? */
6194 } ctdb_commands[] = {
6195 { "version", control_version, true, true, "show version of ctdb" },
6196 { "status", control_status, true, false, "show node status" },
6197 { "uptime", control_uptime, true, false, "show node uptime" },
6198 { "ping", control_ping, true, false, "ping all nodes" },
6199 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6200 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6201 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6202 { "listvars", control_listvars, true, false, "list tunable variables"},
6203 { "statistics", control_statistics, false, false, "show statistics" },
6204 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6205 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6206 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6207 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6208 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6209 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6210 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6211 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6212 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6213 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6214 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6215 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6216 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6217 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6218 { "lvs", control_lvs, true, false, "show lvs configuration" },
6219 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6220 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6221 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6222 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6223 { "getdebug", control_getdebug, true, false, "get debug level" },
6224 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6225 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6226 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6227 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6228 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6229 { "disable", control_disable, true, false, "disable a nodes public IP" },
6230 { "enable", control_enable, true, false, "enable a nodes public IP" },
6231 { "stop", control_stop, true, false, "stop a node" },
6232 { "continue", control_continue, true, false, "re-start a stopped node" },
6233 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6234 { "unban", control_unban, true, false, "unban a node" },
6235 { "showban", control_showban, true, false, "show ban information"},
6236 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6237 { "recover", control_recover, true, false, "force recovery" },
6238 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6239 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6240 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6241 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6242 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6243 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6244 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6245 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6246 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6248 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6250 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6251 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6252 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6253 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6254 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6255 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6256 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6257 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6258 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6259 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6260 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6261 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6262 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6263 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6264 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6265 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6266 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6267 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6268 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6269 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6270 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6271 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6272 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6273 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6274 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6275 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6276 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6277 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6278 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6279 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6280 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6281 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6282 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6283 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6284 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6285 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6286 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6287 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6288 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6289 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6290 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6291 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6292 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6293 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6294 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6295 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6296 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6297 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6298 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6304 static void usage(void)
6308 "Usage: ctdb [options] <control>\n" \
6310 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6311 " -Y generate machine readable output\n"
6312 " -x <char> specify delimiter for machine readable output\n"
6313 " -v generate verbose output\n"
6314 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6315 printf("Controls:\n");
6316 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6317 printf(" %-15s %-27s %s\n",
6318 ctdb_commands[i].name,
6319 ctdb_commands[i].args?ctdb_commands[i].args:"",
6320 ctdb_commands[i].msg);
6326 static void ctdb_alarm(int sig)
6328 printf("Maximum runtime exceeded - exiting\n");
6335 int main(int argc, const char *argv[])
6337 struct ctdb_context *ctdb;
6338 char *nodestring = NULL;
6339 int machineparsable = 0;
6340 struct poptOption popt_options[] = {
6343 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6344 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6345 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machine readable output", NULL },
6346 { NULL, 'x', POPT_ARG_STRING, &options.machineseparator, 0, "specify separator for machine readable output", "char" },
6347 { NULL, 'X', POPT_ARG_NONE, &machineparsable, 0, "enable machine parsable output with separator |", NULL },
6348 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6349 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6350 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6351 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6352 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6353 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6354 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6358 const char **extra_argv;
6362 struct event_context *ev;
6363 const char *control;
6367 /* set some defaults */
6368 options.maxruntime = 0;
6369 options.timelimit = 10;
6370 options.pnn = CTDB_CURRENT_NODE;
6372 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6374 while ((opt = poptGetNextOpt(pc)) != -1) {
6377 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6378 poptBadOption(pc, 0), poptStrerror(opt)));
6383 /* setup the remaining options for the main program to use */
6384 extra_argv = poptGetArgs(pc);
6387 while (extra_argv[extra_argc]) extra_argc++;
6390 if (extra_argc < 1) {
6394 if (options.maxruntime == 0) {
6395 const char *ctdb_timeout;
6396 ctdb_timeout = getenv("CTDB_TIMEOUT");
6397 if (ctdb_timeout != NULL) {
6398 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6400 /* default timeout is 120 seconds */
6401 options.maxruntime = 120;
6405 if (machineparsable) {
6406 options.machineseparator = "|";
6408 if (options.machineseparator != NULL) {
6409 if (strlen(options.machineseparator) != 1) {
6410 printf("Invalid separator \"%s\" - "
6411 "must be single character\n",
6412 options.machineseparator);
6417 options.machinereadable = true;
6418 } else if (options.machinereadable) {
6419 options.machineseparator = ":";
6422 signal(SIGALRM, ctdb_alarm);
6423 alarm(options.maxruntime);
6425 control = extra_argv[0];
6427 /* Default value for CTDB_BASE - don't override */
6428 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6430 ev = event_context_init(NULL);
6432 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6436 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6437 if (strcmp(control, ctdb_commands[i].name) == 0) {
6442 if (i == ARRAY_SIZE(ctdb_commands)) {
6443 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6447 if (ctdb_commands[i].without_daemon == true) {
6448 if (nodestring != NULL) {
6449 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6452 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6455 /* initialise ctdb */
6456 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6460 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6462 pnn = find_node_xpnn();
6465 ("Is this node part of a CTDB cluster?\n"));
6470 /* setup the node number(s) to contact */
6471 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6472 &options.nodes, &options.pnn)) {
6476 if (options.pnn == CTDB_CURRENT_NODE) {
6477 options.pnn = options.nodes[0];
6480 if (ctdb_commands[i].auto_all &&
6481 ((options.pnn == CTDB_BROADCAST_ALL) ||
6482 (options.pnn == CTDB_MULTICAST))) {
6486 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6487 options.pnn = options.nodes[j];
6488 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6491 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6496 (void)poptFreeContext(pc);