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);
48 int printemptyrecords;
55 #define LONGTIMEOUT options.timelimit*10
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
60 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
62 return (tv2->tv_sec - tv->tv_sec) +
63 (tv2->tv_usec - tv->tv_usec)*1.0e-6;
66 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
68 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
72 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
73 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
74 "Out of memory in " __location__ )); \
78 static uint32_t getpnn(struct ctdb_context *ctdb)
80 if ((options.pnn == CTDB_BROADCAST_ALL) ||
81 (options.pnn == CTDB_MULTICAST)) {
83 ("Cannot get PNN for node %u\n", options.pnn));
87 if (options.pnn == CTDB_CURRENT_NODE) {
88 return ctdb_get_pnn(ctdb);
94 static void assert_single_node_only(void)
96 if ((options.pnn == CTDB_BROADCAST_ALL) ||
97 (options.pnn == CTDB_MULTICAST)) {
99 ("This control can not be applied to multiple PNNs\n"));
104 /* Pretty print the flags to a static buffer in human-readable format.
105 * This never returns NULL!
107 static const char *pretty_print_flags(uint32_t flags)
110 static const struct {
114 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
115 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
116 { NODE_FLAGS_BANNED, "BANNED" },
117 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
118 { NODE_FLAGS_DELETED, "DELETED" },
119 { NODE_FLAGS_STOPPED, "STOPPED" },
120 { NODE_FLAGS_INACTIVE, "INACTIVE" },
122 static char flags_str[512]; /* Big enough to contain all flag names */
125 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
126 if (flags & flag_names[j].flag) {
127 if (flags_str[0] == '\0') {
128 (void) strcpy(flags_str, flag_names[j].name);
130 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
131 (void) strncat(flags_str, flag_names[j].name,
132 sizeof(flags_str)-1);
136 if (flags_str[0] == '\0') {
137 (void) strcpy(flags_str, "OK");
143 static int h2i(char h)
145 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
146 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
150 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
153 TDB_DATA key = {NULL, 0};
157 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
162 key.dptr = talloc_size(mem_ctx, key.dsize);
164 for (i=0; i < len/2; i++) {
165 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
170 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
171 * that are disconnected or deleted. If dd_ok is true those nodes are
172 * included in the output list of nodes. If dd_ok is false, those
173 * nodes are filtered from the "all" case and cause an error if
174 * explicitly specified.
176 static bool parse_nodestring(struct ctdb_context *ctdb,
178 const char * nodestring,
179 uint32_t current_pnn,
184 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
187 struct ctdb_node_map *nodemap;
192 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
194 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
195 talloc_free(tmp_ctx);
199 if (nodestring != NULL) {
200 *nodes = talloc_array(mem_ctx, uint32_t, 0);
201 if (*nodes == NULL) {
207 if (strcmp(nodestring, "all") == 0) {
208 *pnn_mode = CTDB_BROADCAST_ALL;
211 for (i = 0; i < nodemap->num; i++) {
212 if ((nodemap->nodes[i].flags &
213 (NODE_FLAGS_DISCONNECTED |
214 NODE_FLAGS_DELETED)) && !dd_ok) {
217 *nodes = talloc_realloc(mem_ctx, *nodes,
219 if (*nodes == NULL) {
229 ns = talloc_strdup(tmp_ctx, nodestring);
230 tok = strtok(ns, ",");
231 while (tok != NULL) {
234 i = (uint32_t)strtoul(tok, &endptr, 0);
235 if (i == 0 && tok == endptr) {
237 ("Invalid node %s\n", tok));
238 talloc_free(tmp_ctx);
241 if (i >= nodemap->num) {
242 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
243 talloc_free(tmp_ctx);
246 if ((nodemap->nodes[i].flags &
247 (NODE_FLAGS_DISCONNECTED |
248 NODE_FLAGS_DELETED)) && !dd_ok) {
249 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
250 talloc_free(tmp_ctx);
253 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
254 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
255 talloc_free(tmp_ctx);
259 *nodes = talloc_realloc(mem_ctx, *nodes,
261 if (*nodes == NULL) {
268 tok = strtok(NULL, ",");
273 *pnn_mode = (*nodes)[0];
275 *pnn_mode = CTDB_MULTICAST;
279 /* default - no nodes specified */
280 *nodes = talloc_array(mem_ctx, uint32_t, 1);
281 if (*nodes == NULL) {
284 *pnn_mode = CTDB_CURRENT_NODE;
286 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
291 talloc_free(tmp_ctx);
295 talloc_free(tmp_ctx);
300 check if a database exists
302 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
303 uint32_t *dbid, const char **dbname, uint8_t *flags)
306 struct ctdb_dbid_map *dbmap=NULL;
307 bool dbid_given = false, found = false;
309 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
312 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
314 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
318 if (strncmp(dbarg, "0x", 2) == 0) {
319 id = strtoul(dbarg, NULL, 0);
323 for(i=0; i<dbmap->num; i++) {
325 if (id == dbmap->dbs[i].dbid) {
330 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
332 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
336 if (strcmp(name, dbarg) == 0) {
337 id = dbmap->dbs[i].dbid;
344 if (found && dbid_given && dbname != NULL) {
345 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
347 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
354 if (dbid) *dbid = id;
355 if (dbname) *dbname = talloc_strdup(ctdb, name);
356 if (flags) *flags = dbmap->dbs[i].flags;
358 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
362 talloc_free(tmp_ctx);
367 see if a process exists
369 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
377 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
378 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
382 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
384 printf("%u:%u exists\n", pnn, pid);
386 printf("%u:%u does not exist\n", pnn, pid);
392 display statistics structure
394 static void show_statistics(struct ctdb_statistics *s, int show_header)
396 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
398 const char *prefix=NULL;
400 int tmp, days, hours, minutes, seconds;
405 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
406 STATISTICS_FIELD(num_clients),
407 STATISTICS_FIELD(frozen),
408 STATISTICS_FIELD(recovering),
409 STATISTICS_FIELD(num_recoveries),
410 STATISTICS_FIELD(client_packets_sent),
411 STATISTICS_FIELD(client_packets_recv),
412 STATISTICS_FIELD(node_packets_sent),
413 STATISTICS_FIELD(node_packets_recv),
414 STATISTICS_FIELD(keepalive_packets_sent),
415 STATISTICS_FIELD(keepalive_packets_recv),
416 STATISTICS_FIELD(node.req_call),
417 STATISTICS_FIELD(node.reply_call),
418 STATISTICS_FIELD(node.req_dmaster),
419 STATISTICS_FIELD(node.reply_dmaster),
420 STATISTICS_FIELD(node.reply_error),
421 STATISTICS_FIELD(node.req_message),
422 STATISTICS_FIELD(node.req_control),
423 STATISTICS_FIELD(node.reply_control),
424 STATISTICS_FIELD(client.req_call),
425 STATISTICS_FIELD(client.req_message),
426 STATISTICS_FIELD(client.req_control),
427 STATISTICS_FIELD(timeouts.call),
428 STATISTICS_FIELD(timeouts.control),
429 STATISTICS_FIELD(timeouts.traverse),
430 STATISTICS_FIELD(locks.num_calls),
431 STATISTICS_FIELD(locks.num_current),
432 STATISTICS_FIELD(locks.num_pending),
433 STATISTICS_FIELD(locks.num_failed),
434 STATISTICS_FIELD(total_calls),
435 STATISTICS_FIELD(pending_calls),
436 STATISTICS_FIELD(childwrite_calls),
437 STATISTICS_FIELD(pending_childwrite_calls),
438 STATISTICS_FIELD(memory_used),
439 STATISTICS_FIELD(max_hop_count),
440 STATISTICS_FIELD(total_ro_delegations),
441 STATISTICS_FIELD(total_ro_revokes),
444 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
453 if (options.machinereadable){
455 printf("CTDB version:");
456 printf("Current time of statistics:");
457 printf("Statistics collected since:");
458 for (i=0;i<ARRAY_SIZE(fields);i++) {
459 printf("%s:", fields[i].name);
461 printf("num_reclock_ctdbd_latency:");
462 printf("min_reclock_ctdbd_latency:");
463 printf("avg_reclock_ctdbd_latency:");
464 printf("max_reclock_ctdbd_latency:");
466 printf("num_reclock_recd_latency:");
467 printf("min_reclock_recd_latency:");
468 printf("avg_reclock_recd_latency:");
469 printf("max_reclock_recd_latency:");
471 printf("num_call_latency:");
472 printf("min_call_latency:");
473 printf("avg_call_latency:");
474 printf("max_call_latency:");
476 printf("num_lockwait_latency:");
477 printf("min_lockwait_latency:");
478 printf("avg_lockwait_latency:");
479 printf("max_lockwait_latency:");
481 printf("num_childwrite_latency:");
482 printf("min_childwrite_latency:");
483 printf("avg_childwrite_latency:");
484 printf("max_childwrite_latency:");
487 printf("%d:", CTDB_VERSION);
488 printf("%d:", (int)s->statistics_current_time.tv_sec);
489 printf("%d:", (int)s->statistics_start_time.tv_sec);
490 for (i=0;i<ARRAY_SIZE(fields);i++) {
491 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
493 printf("%d:", s->reclock.ctdbd.num);
494 printf("%.6f:", s->reclock.ctdbd.min);
495 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
496 printf("%.6f:", s->reclock.ctdbd.max);
498 printf("%d:", s->reclock.recd.num);
499 printf("%.6f:", s->reclock.recd.min);
500 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
501 printf("%.6f:", s->reclock.recd.max);
503 printf("%d:", s->call_latency.num);
504 printf("%.6f:", s->call_latency.min);
505 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
506 printf("%.6f:", s->call_latency.max);
508 printf("%d:", s->childwrite_latency.num);
509 printf("%.6f:", s->childwrite_latency.min);
510 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
511 printf("%.6f:", s->childwrite_latency.max);
514 printf("CTDB version %u\n", CTDB_VERSION);
515 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
516 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
518 for (i=0;i<ARRAY_SIZE(fields);i++) {
519 if (strchr(fields[i].name, '.')) {
520 preflen = strcspn(fields[i].name, ".")+1;
521 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
522 prefix = fields[i].name;
523 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
528 printf(" %*s%-22s%*s%10u\n",
530 fields[i].name+preflen,
532 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
534 printf(" hop_count_buckets:");
535 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
536 printf(" %d", s->hop_count_bucket[i]);
539 printf(" lock_buckets:");
540 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
541 printf(" %d", s->locks.buckets[i]);
544 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);
546 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);
548 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);
550 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);
551 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);
554 talloc_free(tmp_ctx);
558 display remote ctdb statistics combined from all nodes
560 static int control_statistics_all(struct ctdb_context *ctdb)
563 struct ctdb_statistics statistics;
567 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
568 CTDB_NO_MEMORY(ctdb, nodes);
570 ZERO_STRUCT(statistics);
572 for (i=0;i<num_nodes;i++) {
573 struct ctdb_statistics s1;
575 uint32_t *v1 = (uint32_t *)&s1;
576 uint32_t *v2 = (uint32_t *)&statistics;
578 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
579 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
581 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
584 for (j=0;j<num_ints;j++) {
587 statistics.max_hop_count =
588 MAX(statistics.max_hop_count, s1.max_hop_count);
589 statistics.call_latency.max =
590 MAX(statistics.call_latency.max, s1.call_latency.max);
593 printf("Gathered statistics for %u nodes\n", num_nodes);
594 show_statistics(&statistics, 1);
599 display remote ctdb statistics
601 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
604 struct ctdb_statistics statistics;
606 if (options.pnn == CTDB_BROADCAST_ALL) {
607 return control_statistics_all(ctdb);
610 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
612 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
615 show_statistics(&statistics, 1);
621 reset remote ctdb statistics
623 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
627 ret = ctdb_statistics_reset(ctdb, options.pnn);
629 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
637 display remote ctdb rolling statistics
639 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
642 struct ctdb_statistics_wire *stats;
643 int i, num_records = -1;
645 assert_single_node_only();
648 num_records = atoi(argv[0]) - 1;
651 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
653 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
656 for (i=0;i<stats->num;i++) {
657 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
660 show_statistics(&stats->stats[i], i==0);
661 if (i == num_records) {
670 display remote ctdb db statistics
672 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
674 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
675 struct ctdb_db_statistics *dbstat;
685 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
689 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
691 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
692 talloc_free(tmp_ctx);
696 printf("DB Statistics: %s\n", argv[0]);
697 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
698 dbstat->db_ro_delegations);
699 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
700 dbstat->db_ro_delegations);
701 printf(" %s\n", "locks");
702 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
703 dbstat->locks.num_calls);
704 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
705 dbstat->locks.num_failed);
706 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
707 dbstat->locks.num_current);
708 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
709 dbstat->locks.num_pending);
710 printf(" %s", "hop_count_buckets:");
711 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
712 printf(" %d", dbstat->hop_count_bucket[i]);
715 printf(" %s", "lock_buckets:");
716 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
717 printf(" %d", dbstat->locks.buckets[i]);
720 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
721 "locks_latency MIN/AVG/MAX",
722 dbstat->locks.latency.min,
723 (dbstat->locks.latency.num ?
724 dbstat->locks.latency.total /dbstat->locks.latency.num :
726 dbstat->locks.latency.max,
727 dbstat->locks.latency.num);
729 for (i=0; i<dbstat->num_hot_keys; i++) {
730 if (dbstat->hot_keys[i].count > 0) {
734 dbstat->num_hot_keys = num_hot_keys;
736 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
737 for (i = 0; i < dbstat->num_hot_keys; i++) {
739 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
740 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
741 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
746 talloc_free(tmp_ctx);
751 display uptime of remote node
753 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
756 struct ctdb_uptime *uptime = NULL;
757 int tmp, days, hours, minutes, seconds;
759 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
761 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
765 if (options.machinereadable){
766 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
767 printf(":%u:%u:%u:%lf\n",
768 (unsigned int)uptime->current_time.tv_sec,
769 (unsigned int)uptime->ctdbd_start_time.tv_sec,
770 (unsigned int)uptime->last_recovery_finished.tv_sec,
771 timeval_delta(&uptime->last_recovery_finished,
772 &uptime->last_recovery_started)
777 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
779 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
787 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
789 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
797 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
799 printf("Duration of last recovery/failover: %lf seconds\n",
800 timeval_delta(&uptime->last_recovery_finished,
801 &uptime->last_recovery_started));
807 show the PNN of the current node
809 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
813 mypnn = getpnn(ctdb);
815 printf("PNN:%d\n", mypnn);
821 struct pnn_node *next, *prev;
826 static struct pnn_node *read_pnn_node_file(TALLOC_CTX *mem_ctx,
832 struct pnn_node *pnn_nodes = NULL;
833 struct pnn_node *pnn_node;
835 lines = file_lines_load(file, &nlines, 0, mem_ctx);
839 for (i=0, pnn=0; i<nlines; i++) {
843 /* strip leading spaces */
844 while((*node == ' ') || (*node == '\t')) {
851 if (strcmp(node, "") == 0) {
854 pnn_node = talloc(mem_ctx, struct pnn_node);
855 pnn_node->pnn = pnn++;
857 if (!parse_ip(node, NULL, 0, &pnn_node->addr)) {
859 ("Invalid IP address '%s' in file %s\n",
861 /* Caller will free mem_ctx */
865 DLIST_ADD_END(pnn_nodes, pnn_node, NULL);
871 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
873 const char *nodes_list;
875 /* read the nodes file */
876 nodes_list = getenv("CTDB_NODES");
877 if (nodes_list == NULL) {
878 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
879 getenv("CTDB_BASE"));
880 if (nodes_list == NULL) {
881 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
886 return read_pnn_node_file(mem_ctx, nodes_list);
890 show the PNN of the current node
891 discover the pnn by loading the nodes file and try to bind to all
892 addresses one at a time until the ip address is found.
894 static int find_node_xpnn(void)
896 TALLOC_CTX *mem_ctx = talloc_new(NULL);
897 struct pnn_node *pnn_nodes;
898 struct pnn_node *pnn_node;
901 pnn_nodes = read_nodes_file(mem_ctx);
902 if (pnn_nodes == NULL) {
903 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
904 talloc_free(mem_ctx);
908 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
909 if (ctdb_sys_have_ip(&pnn_node->addr)) {
911 talloc_free(mem_ctx);
916 printf("Failed to detect which PNN this node is\n");
917 talloc_free(mem_ctx);
921 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
925 assert_single_node_only();
927 pnn = find_node_xpnn();
932 printf("PNN:%d\n", pnn);
936 /* Helpers for ctdb status
938 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
940 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
944 if (node->flags == 0) {
945 struct ctdb_control_get_ifaces *ifaces;
947 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
948 tmp_ctx, &ifaces) == 0) {
949 for (j=0; j < ifaces->num; j++) {
950 if (ifaces->ifaces[j].link_state != 0) {
958 talloc_free(tmp_ctx);
963 static void control_status_header_machine(void)
965 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
966 ":Inactive:PartiallyOnline:ThisNode:\n");
969 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
970 struct ctdb_node_and_flags *node)
972 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
973 ctdb_addr_to_str(&node->addr),
974 !!(node->flags&NODE_FLAGS_DISCONNECTED),
975 !!(node->flags&NODE_FLAGS_BANNED),
976 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
977 !!(node->flags&NODE_FLAGS_UNHEALTHY),
978 !!(node->flags&NODE_FLAGS_STOPPED),
979 !!(node->flags&NODE_FLAGS_INACTIVE),
980 is_partially_online(ctdb, node) ? 1 : 0,
981 (node->pnn == mypnn)?'Y':'N');
986 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
987 struct ctdb_node_and_flags *node)
989 printf("pnn:%d %-16s %s%s\n", node->pnn,
990 ctdb_addr_to_str(&node->addr),
991 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
992 node->pnn == mypnn?" (THIS NODE)":"");
998 display remote ctdb status
1000 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
1002 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1004 struct ctdb_vnn_map *vnnmap=NULL;
1005 struct ctdb_node_map *nodemap=NULL;
1006 uint32_t recmode, recmaster, mypnn;
1007 int num_deleted_nodes = 0;
1010 mypnn = getpnn(ctdb);
1012 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1014 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1015 talloc_free(tmp_ctx);
1019 if (options.machinereadable) {
1020 control_status_header_machine();
1021 for (i=0;i<nodemap->num;i++) {
1022 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1025 (void) control_status_1_machine(ctdb, mypnn,
1026 &nodemap->nodes[i]);
1028 talloc_free(tmp_ctx);
1032 for (i=0; i<nodemap->num; i++) {
1033 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1034 num_deleted_nodes++;
1037 if (num_deleted_nodes == 0) {
1038 printf("Number of nodes:%d\n", nodemap->num);
1040 printf("Number of nodes:%d (including %d deleted nodes)\n",
1041 nodemap->num, num_deleted_nodes);
1043 for(i=0;i<nodemap->num;i++){
1044 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1047 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1050 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1052 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1053 talloc_free(tmp_ctx);
1056 if (vnnmap->generation == INVALID_GENERATION) {
1057 printf("Generation:INVALID\n");
1059 printf("Generation:%d\n",vnnmap->generation);
1061 printf("Size:%d\n",vnnmap->size);
1062 for(i=0;i<vnnmap->size;i++){
1063 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1066 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1068 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1069 talloc_free(tmp_ctx);
1072 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1074 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1076 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1077 talloc_free(tmp_ctx);
1080 printf("Recovery master:%d\n",recmaster);
1082 talloc_free(tmp_ctx);
1086 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1088 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1090 struct ctdb_node_map *nodemap=NULL;
1092 uint32_t pnn_mode, mypnn;
1098 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1099 options.pnn, true, &nodes, &pnn_mode)) {
1103 if (options.machinereadable) {
1104 control_status_header_machine();
1105 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1106 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1109 mypnn = getpnn(ctdb);
1111 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1113 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1114 talloc_free(tmp_ctx);
1120 for (i = 0; i < talloc_array_length(nodes); i++) {
1121 if (options.machinereadable) {
1122 ret |= control_status_1_machine(ctdb, mypnn,
1123 &nodemap->nodes[nodes[i]]);
1125 ret |= control_status_1_human(ctdb, mypnn,
1126 &nodemap->nodes[nodes[i]]);
1130 talloc_free(tmp_ctx);
1134 static struct pnn_node *read_natgw_nodes_file(struct ctdb_context *ctdb,
1135 TALLOC_CTX *mem_ctx)
1137 const char *natgw_list;
1138 struct pnn_node *natgw_nodes = NULL;
1140 natgw_list = getenv("CTDB_NATGW_NODES");
1141 if (natgw_list == NULL) {
1142 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1143 getenv("CTDB_BASE"));
1144 if (natgw_list == NULL) {
1145 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1149 /* The PNNs will be junk but they're not used */
1150 natgw_nodes = read_pnn_node_file(mem_ctx, natgw_list);
1151 if (natgw_nodes == NULL) {
1153 ("Failed to load natgw node list '%s'\n", natgw_list));
1159 /* talloc off the existing nodemap... */
1160 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1162 return talloc_zero_size(nodemap,
1163 offsetof(struct ctdb_node_map, nodes) +
1164 nodemap->num * sizeof(struct ctdb_node_and_flags));
1167 static struct ctdb_node_map *
1168 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1169 struct ctdb_node_map *nodemap,
1170 struct pnn_node *nodes)
1174 struct ctdb_node_map *ret;
1176 ret = talloc_nodemap(nodemap);
1177 CTDB_NO_MEMORY_NULL(ctdb, ret);
1181 for (i = 0; i < nodemap->num; i++) {
1182 for(n = nodes; n != NULL ; n = n->next) {
1183 if (ctdb_same_ip(&n->addr,
1184 &nodemap->nodes[i].addr)) {
1192 ret->nodes[ret->num] = nodemap->nodes[i];
1199 static struct ctdb_node_map *
1200 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1201 struct ctdb_node_map *nodemap,
1202 uint32_t required_capabilities,
1206 uint32_t capabilities;
1207 struct ctdb_node_map *ret;
1209 ret = talloc_nodemap(nodemap);
1210 CTDB_NO_MEMORY_NULL(ctdb, ret);
1214 for (i = 0; i < nodemap->num; i++) {
1217 /* Disconnected nodes have no capabilities! */
1218 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1222 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1223 nodemap->nodes[i].pnn,
1226 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1227 nodemap->nodes[i].pnn));
1231 if (!(capabilities & required_capabilities)) {
1235 ret->nodes[ret->num] = nodemap->nodes[i];
1245 static struct ctdb_node_map *
1246 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1247 struct ctdb_node_map *nodemap,
1248 uint32_t flags_mask)
1251 struct ctdb_node_map *ret;
1253 ret = talloc_nodemap(nodemap);
1254 CTDB_NO_MEMORY_NULL(ctdb, ret);
1258 for (i = 0; i < nodemap->num; i++) {
1259 if (nodemap->nodes[i].flags & flags_mask) {
1263 ret->nodes[ret->num] = nodemap->nodes[i];
1271 display the list of nodes belonging to this natgw configuration
1273 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1275 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1277 struct pnn_node *natgw_nodes = NULL;
1278 struct ctdb_node_map *orig_nodemap=NULL;
1279 struct ctdb_node_map *nodemap;
1280 uint32_t mypnn, pnn;
1283 /* When we have some nodes that could be the NATGW, make a
1284 * series of attempts to find the first node that doesn't have
1285 * certain status flags set.
1287 uint32_t exclude_flags[] = {
1288 /* Look for a nice healthy node */
1289 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1290 /* If not found, an UNHEALTHY/BANNED node will do */
1291 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1292 /* If not found, a STOPPED node will do */
1293 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1297 /* read the natgw nodes file into a linked list */
1298 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1299 if (natgw_nodes == NULL) {
1304 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1305 tmp_ctx, &orig_nodemap);
1307 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1308 talloc_free(tmp_ctx);
1312 /* Get a nodemap that includes only the nodes in the NATGW
1314 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1315 if (nodemap == NULL) {
1320 ret = 2; /* matches ENOENT */
1323 /* For each flag mask... */
1324 for (i = 0; exclude_flags[i] != 0; i++) {
1325 /* ... get a nodemap that excludes nodes with with
1326 * masked flags... */
1327 struct ctdb_node_map *t =
1328 filter_nodemap_by_flags(ctdb, nodemap,
1336 /* ... and find the first node with the NATGW
1338 struct ctdb_node_map *n;
1339 n = filter_nodemap_by_capabilities(ctdb, t,
1349 pnn = n->nodes[0].pnn;
1350 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1357 if (options.machinereadable) {
1358 printf(":Node:IP:\n");
1359 printf(":%d:%s:\n", pnn, ip);
1361 printf("%d %s\n", pnn, ip);
1364 /* print the pruned list of nodes belonging to this natgw list */
1365 mypnn = getpnn(ctdb);
1366 if (options.machinereadable) {
1367 control_status_header_machine();
1369 printf("Number of nodes:%d\n", nodemap->num);
1371 for(i=0;i<nodemap->num;i++){
1372 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1375 if (options.machinereadable) {
1376 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1378 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1383 talloc_free(tmp_ctx);
1388 display the status of the scripts for monitoring (or other events)
1390 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1391 enum ctdb_eventscript_call type)
1393 struct ctdb_scripts_wire *script_status;
1396 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1398 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1402 if (script_status == NULL) {
1403 if (!options.machinereadable) {
1404 printf("%s cycle never run\n",
1405 ctdb_eventscript_call_names[type]);
1410 if (!options.machinereadable) {
1412 for (i=0; i<script_status->num_scripts; i++) {
1413 if (script_status->scripts[i].status != -ENOEXEC) {
1417 printf("%d scripts were executed last %s cycle\n",
1419 ctdb_eventscript_call_names[type]);
1421 for (i=0; i<script_status->num_scripts; i++) {
1422 const char *status = NULL;
1424 switch (script_status->scripts[i].status) {
1426 status = "TIMEDOUT";
1429 status = "DISABLED";
1435 if (script_status->scripts[i].status > 0)
1439 if (options.machinereadable) {
1440 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1441 ctdb_eventscript_call_names[type],
1442 script_status->scripts[i].name,
1443 script_status->scripts[i].status,
1445 (long)script_status->scripts[i].start.tv_sec,
1446 (long)script_status->scripts[i].start.tv_usec,
1447 (long)script_status->scripts[i].finished.tv_sec,
1448 (long)script_status->scripts[i].finished.tv_usec,
1449 script_status->scripts[i].output);
1453 printf("%-20s Status:%s ",
1454 script_status->scripts[i].name, status);
1456 /* Some other error, eg from stat. */
1457 printf("%-20s Status:CANNOT RUN (%s)",
1458 script_status->scripts[i].name,
1459 strerror(-script_status->scripts[i].status));
1461 if (script_status->scripts[i].status >= 0) {
1462 printf("Duration:%.3lf ",
1463 timeval_delta(&script_status->scripts[i].finished,
1464 &script_status->scripts[i].start));
1466 if (script_status->scripts[i].status != -ENOEXEC) {
1468 ctime(&script_status->scripts[i].start.tv_sec));
1469 if (script_status->scripts[i].status != 0) {
1470 printf(" OUTPUT:%s\n",
1471 script_status->scripts[i].output);
1481 static int control_scriptstatus(struct ctdb_context *ctdb,
1482 int argc, const char **argv)
1485 enum ctdb_eventscript_call type, min, max;
1489 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1494 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1498 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1499 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1505 if (type == CTDB_EVENT_MAX) {
1506 if (strcmp(arg, "all") == 0) {
1508 max = CTDB_EVENT_MAX;
1510 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1515 if (options.machinereadable) {
1516 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1519 for (type = min; type < max; type++) {
1520 ret = control_one_scriptstatus(ctdb, type);
1530 enable an eventscript
1532 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1540 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1542 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1550 disable an eventscript
1552 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1560 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1562 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1570 display the pnn of the recovery master
1572 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1577 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1579 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1582 printf("%d\n",recmaster);
1588 add a tickle to a public address
1590 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1592 struct ctdb_tcp_connection t;
1596 assert_single_node_only();
1602 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1603 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1606 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1607 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1611 data.dptr = (uint8_t *)&t;
1612 data.dsize = sizeof(t);
1614 /* tell all nodes about this tcp connection */
1615 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1616 0, data, ctdb, NULL, NULL, NULL, NULL);
1618 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1627 delete a tickle from a node
1629 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1631 struct ctdb_tcp_connection t;
1635 assert_single_node_only();
1641 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1642 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1645 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1646 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1650 data.dptr = (uint8_t *)&t;
1651 data.dsize = sizeof(t);
1653 /* tell all nodes about this tcp connection */
1654 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1655 0, data, ctdb, NULL, NULL, NULL, NULL);
1657 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1666 get a list of all tickles for this pnn
1668 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1670 struct ctdb_control_tcp_tickle_list *list;
1671 ctdb_sock_addr addr;
1675 assert_single_node_only();
1682 port = atoi(argv[1]);
1685 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1686 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1690 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1692 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1696 if (options.machinereadable){
1697 printf(":source ip:port:destination ip:port:\n");
1698 for (i=0;i<list->tickles.num;i++) {
1699 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1702 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1703 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1706 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1707 printf("Num tickles:%u\n", list->tickles.num);
1708 for (i=0;i<list->tickles.num;i++) {
1709 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1712 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1713 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));
1723 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1725 struct ctdb_all_public_ips *ips;
1726 struct ctdb_public_ip ip;
1729 uint32_t disable_time;
1731 struct ctdb_node_map *nodemap=NULL;
1732 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1735 data.dptr = (uint8_t*)&disable_time;
1736 data.dsize = sizeof(disable_time);
1737 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1739 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1745 /* read the public ip list from the node */
1746 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1748 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1749 talloc_free(tmp_ctx);
1753 for (i=0;i<ips->num;i++) {
1754 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1759 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1760 pnn, ctdb_addr_to_str(addr)));
1761 talloc_free(tmp_ctx);
1768 data.dptr = (uint8_t *)&ip;
1769 data.dsize = sizeof(ip);
1771 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1773 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1774 talloc_free(tmp_ctx);
1778 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1779 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1786 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1787 talloc_free(tmp_ctx);
1791 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1793 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1794 talloc_free(tmp_ctx);
1798 /* update the recovery daemon so it now knows to expect the new
1799 node assignment for this ip.
1801 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1803 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1807 talloc_free(tmp_ctx);
1813 * scans all other nodes and returns a pnn for another node that can host this
1817 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1819 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1820 struct ctdb_all_public_ips *ips;
1821 struct ctdb_node_map *nodemap=NULL;
1825 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1827 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1828 talloc_free(tmp_ctx);
1832 for(i=0;i<nodemap->num;i++){
1833 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1836 if (nodemap->nodes[i].pnn == options.pnn) {
1840 /* read the public ip list from this node */
1841 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1843 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1847 for (j=0;j<ips->num;j++) {
1848 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1849 pnn = nodemap->nodes[i].pnn;
1850 talloc_free(tmp_ctx);
1857 talloc_free(tmp_ctx);
1861 /* If pnn is -1 then try to find a node to move IP to... */
1862 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1864 bool pnn_specified = (pnn == -1 ? false : true);
1867 while (retries < 5) {
1868 if (!pnn_specified) {
1869 pnn = find_other_host_for_public_ip(ctdb, addr);
1874 ("Trying to move public IP to node %u\n", pnn));
1877 if (move_ip(ctdb, addr, pnn) == 0) {
1890 move/failover an ip address to a specific node
1892 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1895 ctdb_sock_addr addr;
1897 assert_single_node_only();
1904 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1905 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1910 if (sscanf(argv[1], "%u", &pnn) != 1) {
1911 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1915 if (!try_moveip(ctdb, &addr, pnn)) {
1916 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1923 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1927 data.dptr = (uint8_t *)&pnn;
1928 data.dsize = sizeof(uint32_t);
1929 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1931 ("Failed to send message to force node %u to be a rebalancing target\n",
1941 rebalance a node by setting it to allow failback and triggering a
1944 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1946 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1951 assert_single_node_only();
1957 /* Determine the nodes where IPs need to be reloaded */
1958 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1959 options.pnn, true, &nodes, &pnn_mode)) {
1964 for (i = 0; i < talloc_array_length(nodes); i++) {
1965 if (!rebalance_node(ctdb, nodes[i])) {
1971 talloc_free(tmp_ctx);
1975 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1977 struct ctdb_public_ip ip;
1980 uint32_t disable_time;
1982 struct ctdb_node_map *nodemap=NULL;
1983 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1986 data.dptr = (uint8_t*)&disable_time;
1987 data.dsize = sizeof(disable_time);
1988 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1990 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1997 data.dptr = (uint8_t *)&ip;
1998 data.dsize = sizeof(ip);
2000 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
2002 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2003 talloc_free(tmp_ctx);
2007 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2008 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
2015 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
2016 talloc_free(tmp_ctx);
2020 talloc_free(tmp_ctx);
2025 release an ip form all nodes and have it re-assigned by recd
2027 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2029 ctdb_sock_addr addr;
2031 assert_single_node_only();
2038 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2039 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2043 if (rebalance_ip(ctdb, &addr) != 0) {
2044 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2051 static int getips_store_callback(void *param, void *data)
2053 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2054 struct ctdb_all_public_ips *ips = param;
2058 ips->ips[i].pnn = node_ip->pnn;
2059 ips->ips[i].addr = node_ip->addr;
2063 static int getips_count_callback(void *param, void *data)
2065 uint32_t *count = param;
2072 static uint32_t *ip_key(ctdb_sock_addr *ip)
2074 static uint32_t key[IP_KEYLEN];
2076 bzero(key, sizeof(key));
2078 switch (ip->sa.sa_family) {
2080 key[0] = ip->ip.sin_addr.s_addr;
2083 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2091 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2098 static void *add_ip_callback(void *parm, void *data)
2104 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2106 struct ctdb_all_public_ips *tmp_ips;
2107 struct ctdb_node_map *nodemap=NULL;
2108 trbt_tree_t *ip_tree;
2112 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2114 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2118 ip_tree = trbt_create(tmp_ctx, 0);
2120 for(i=0;i<nodemap->num;i++){
2121 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2124 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2128 /* read the public ip list from this node */
2129 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2131 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2135 for (j=0; j<tmp_ips->num;j++) {
2136 struct ctdb_public_ip *node_ip;
2138 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2139 node_ip->pnn = tmp_ips->ips[j].pnn;
2140 node_ip->addr = tmp_ips->ips[j].addr;
2142 trbt_insertarray32_callback(ip_tree,
2143 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2147 talloc_free(tmp_ips);
2152 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2154 len = offsetof(struct ctdb_all_public_ips, ips) +
2155 count*sizeof(struct ctdb_public_ip);
2156 tmp_ips = talloc_zero_size(tmp_ctx, len);
2157 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2165 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2167 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2169 event_add_timed(ctdb->ev, ctdb,
2170 timeval_current_ofs(1, 0),
2171 ctdb_every_second, ctdb);
2174 struct srvid_reply_handler_data {
2178 const char *srvid_str;
2181 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2186 struct srvid_reply_handler_data *d =
2187 (struct srvid_reply_handler_data *)private_data;
2191 if (data.dsize != sizeof(ret)) {
2192 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2196 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2197 ret = *(int32_t *)data.dptr;
2200 ("%s failed with result %d\n", d->srvid_str, ret));
2204 if (!d->wait_for_all) {
2209 /* Wait for all replies */
2211 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2212 if (d->nodes[i] == ret) {
2214 ("%s reply received from node %u\n",
2215 d->srvid_str, ret));
2218 if (d->nodes[i] != -1) {
2219 /* Found a node that hasn't yet replied */
2225 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2226 * or replies from all connected nodes. arg is the data argument to
2227 * pass in the srvid_request structure - pass 0 if this isn't needed.
2229 static int srvid_broadcast(struct ctdb_context *ctdb,
2230 uint64_t srvid, uint32_t *arg,
2231 const char *srvid_str, bool wait_for_all)
2236 uint64_t reply_srvid;
2237 struct srvid_request request;
2238 struct srvid_request_data request_data;
2239 struct srvid_reply_handler_data reply_data;
2242 ZERO_STRUCT(request);
2244 /* Time ticks to enable timeouts to be processed */
2245 event_add_timed(ctdb->ev, ctdb,
2246 timeval_current_ofs(1, 0),
2247 ctdb_every_second, ctdb);
2249 pnn = ctdb_get_pnn(ctdb);
2250 reply_srvid = getpid();
2254 request.srvid = reply_srvid;
2256 data.dptr = (uint8_t *)&request;
2257 data.dsize = sizeof(request);
2259 request_data.pnn = pnn;
2260 request_data.srvid = reply_srvid;
2261 request_data.data = *arg;
2263 data.dptr = (uint8_t *)&request_data;
2264 data.dsize = sizeof(request_data);
2267 /* Register message port for reply from recovery master */
2268 ctdb_client_set_message_handler(ctdb, reply_srvid,
2269 srvid_broadcast_reply_handler,
2272 reply_data.wait_for_all = wait_for_all;
2273 reply_data.nodes = NULL;
2274 reply_data.srvid_str = srvid_str;
2277 reply_data.done = false;
2280 struct ctdb_node_map *nodemap;
2282 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2283 CTDB_CURRENT_NODE, ctdb, &nodemap);
2286 ("Unable to get nodemap from current node, try again\n"));
2291 if (reply_data.nodes != NULL) {
2292 talloc_free(reply_data.nodes);
2294 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2297 talloc_free(nodemap);
2300 /* Send to all connected nodes. Only recmaster replies */
2301 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2304 /* This can only happen if the socket is closed and
2305 * there's no way to recover from that, so don't try
2309 ("Failed to send %s request to connected nodes\n",
2314 tv = timeval_current();
2315 /* This loop terminates the reply is received */
2316 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2317 event_loop_once(ctdb->ev);
2320 if (!reply_data.done) {
2322 ("Still waiting for confirmation of %s\n", srvid_str));
2327 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2329 talloc_free(reply_data.nodes);
2334 static int ipreallocate(struct ctdb_context *ctdb)
2336 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2337 "IP reallocation", false);
2341 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2343 return ipreallocate(ctdb);
2347 add a public ip address to a node
2349 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2352 int len, retries = 0;
2354 ctdb_sock_addr addr;
2355 struct ctdb_control_ip_iface *pub;
2356 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2357 struct ctdb_all_public_ips *ips;
2361 talloc_free(tmp_ctx);
2365 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2366 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2367 talloc_free(tmp_ctx);
2371 /* read the public ip list from the node */
2372 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2374 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2375 talloc_free(tmp_ctx);
2378 for (i=0;i<ips->num;i++) {
2379 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2380 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2387 /* Dont timeout. This command waits for an ip reallocation
2388 which sometimes can take wuite a while if there has
2389 been a recent recovery
2393 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2394 pub = talloc_size(tmp_ctx, len);
2395 CTDB_NO_MEMORY(ctdb, pub);
2399 pub->len = strlen(argv[1])+1;
2400 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2403 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2405 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2409 } while (retries < 5 && ret != 0);
2411 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2412 talloc_free(tmp_ctx);
2416 if (rebalance_node(ctdb, options.pnn) != 0) {
2417 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2421 talloc_free(tmp_ctx);
2426 add a public ip address to a node
2428 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2430 ctdb_sock_addr addr;
2436 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2437 printf("Badly formed ip : %s\n", argv[0]);
2441 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2446 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2448 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2450 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2451 struct ctdb_node_map *nodemap=NULL;
2452 struct ctdb_all_public_ips *ips;
2455 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2457 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2461 /* remove it from the nodes that are not hosting the ip currently */
2462 for(i=0;i<nodemap->num;i++){
2463 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2466 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2468 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2472 for (j=0;j<ips->num;j++) {
2473 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2481 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2485 options.pnn = nodemap->nodes[i].pnn;
2486 control_delip(ctdb, argc, argv);
2490 /* remove it from every node (also the one hosting it) */
2491 for(i=0;i<nodemap->num;i++){
2492 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2495 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2497 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2501 for (j=0;j<ips->num;j++) {
2502 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2510 options.pnn = nodemap->nodes[i].pnn;
2511 control_delip(ctdb, argc, argv);
2514 talloc_free(tmp_ctx);
2519 delete a public ip address from a node
2521 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2524 ctdb_sock_addr addr;
2525 struct ctdb_control_ip_iface pub;
2526 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2527 struct ctdb_all_public_ips *ips;
2530 talloc_free(tmp_ctx);
2534 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2535 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2539 if (options.pnn == CTDB_BROADCAST_ALL) {
2540 return control_delip_all(ctdb, argc, argv, &addr);
2547 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2549 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2550 talloc_free(tmp_ctx);
2554 for (i=0;i<ips->num;i++) {
2555 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2561 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2562 ctdb_addr_to_str(&addr)));
2563 talloc_free(tmp_ctx);
2567 /* This is an optimisation. If this node is hosting the IP
2568 * then try to move it somewhere else without invoking a full
2569 * takeover run. We don't care if this doesn't work!
2571 if (ips->ips[i].pnn == options.pnn) {
2572 (void) try_moveip(ctdb, &addr, -1);
2575 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2577 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2578 talloc_free(tmp_ctx);
2582 talloc_free(tmp_ctx);
2586 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2587 int argc, const char **argv)
2589 struct ctdb_control_killtcp *killtcp;
2590 int max_entries, current, i;
2591 struct timeval timeout;
2592 char line[128], src[128], dst[128];
2595 struct client_async_data *async_data;
2596 struct ctdb_client_control_state *state;
2606 while (!feof(stdin)) {
2607 if (fgets(line, sizeof(line), stdin) == NULL) {
2611 /* Silently skip empty lines */
2612 if (line[0] == '\n') {
2616 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2617 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2619 talloc_free(killtcp);
2623 if (current >= max_entries) {
2624 max_entries += 1024;
2625 killtcp = talloc_realloc(ctdb, killtcp,
2626 struct ctdb_control_killtcp,
2628 CTDB_NO_MEMORY(ctdb, killtcp);
2631 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2632 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2634 talloc_free(killtcp);
2638 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2639 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2641 talloc_free(killtcp);
2648 async_data = talloc_zero(ctdb, struct client_async_data);
2649 if (async_data == NULL) {
2650 talloc_free(killtcp);
2654 for (i = 0; i < current; i++) {
2656 data.dsize = sizeof(struct ctdb_control_killtcp);
2657 data.dptr = (unsigned char *)&killtcp[i];
2659 timeout = TIMELIMIT();
2660 state = ctdb_control_send(ctdb, options.pnn, 0,
2661 CTDB_CONTROL_KILL_TCP, 0, data,
2662 async_data, &timeout, NULL);
2664 if (state == NULL) {
2666 ("Failed to call async killtcp control to node %u\n",
2668 talloc_free(killtcp);
2672 ctdb_client_async_add(async_data, state);
2675 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2676 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2677 talloc_free(killtcp);
2681 talloc_free(killtcp);
2687 kill a tcp connection
2689 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2692 struct ctdb_control_killtcp killtcp;
2694 assert_single_node_only();
2697 return kill_tcp_from_file(ctdb, argc, argv);
2704 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2705 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2709 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2710 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2714 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2716 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2727 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2730 ctdb_sock_addr addr;
2732 assert_single_node_only();
2738 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2739 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2743 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2745 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2753 register a server id
2755 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2758 struct ctdb_server_id server_id;
2764 server_id.pnn = strtoul(argv[0], NULL, 0);
2765 server_id.type = strtoul(argv[1], NULL, 0);
2766 server_id.server_id = strtoul(argv[2], NULL, 0);
2768 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2770 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2773 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2779 unregister a server id
2781 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2784 struct ctdb_server_id server_id;
2790 server_id.pnn = strtoul(argv[0], NULL, 0);
2791 server_id.type = strtoul(argv[1], NULL, 0);
2792 server_id.server_id = strtoul(argv[2], NULL, 0);
2794 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2796 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2803 check if a server id exists
2805 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2809 struct ctdb_server_id server_id;
2815 server_id.pnn = strtoul(argv[0], NULL, 0);
2816 server_id.type = strtoul(argv[1], NULL, 0);
2817 server_id.server_id = strtoul(argv[2], NULL, 0);
2819 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2821 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2826 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2828 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2834 get a list of all server ids that are registered on a node
2836 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2839 struct ctdb_server_id_list *server_ids;
2841 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2843 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2847 for (i=0; i<server_ids->num; i++) {
2848 printf("Server id %d:%d:%d\n",
2849 server_ids->server_ids[i].pnn,
2850 server_ids->server_ids[i].type,
2851 server_ids->server_ids[i].server_id);
2858 check if a server id exists
2860 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2862 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2868 talloc_free(tmp_ctx);
2872 ids = talloc_array(tmp_ctx, uint64_t, argc);
2873 result = talloc_array(tmp_ctx, uint8_t, argc);
2875 for (i = 0; i < argc; i++) {
2876 ids[i] = strtoull(argv[i], NULL, 0);
2879 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2880 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2882 talloc_free(tmp_ctx);
2886 for (i=0; i < argc; i++) {
2887 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2888 result[i] ? "exists" : "does not exist");
2891 talloc_free(tmp_ctx);
2896 send a tcp tickle ack
2898 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2901 ctdb_sock_addr src, dst;
2907 if (!parse_ip_port(argv[0], &src)) {
2908 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2912 if (!parse_ip_port(argv[1], &dst)) {
2913 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2917 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2921 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2928 display public ip status
2930 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2933 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2934 struct ctdb_all_public_ips *ips;
2936 if (options.pnn == CTDB_BROADCAST_ALL) {
2937 /* read the list of public ips from all nodes */
2938 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2940 /* read the public ip list from this node */
2941 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2944 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2945 talloc_free(tmp_ctx);
2949 if (options.machinereadable){
2950 printf(":Public IP:Node:");
2951 if (options.verbose){
2952 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2956 if (options.pnn == CTDB_BROADCAST_ALL) {
2957 printf("Public IPs on ALL nodes\n");
2959 printf("Public IPs on node %u\n", options.pnn);
2963 for (i=1;i<=ips->num;i++) {
2964 struct ctdb_control_public_ip_info *info = NULL;
2966 char *aciface = NULL;
2967 char *avifaces = NULL;
2968 char *cifaces = NULL;
2970 if (options.pnn == CTDB_BROADCAST_ALL) {
2971 pnn = ips->ips[ips->num-i].pnn;
2977 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2978 &ips->ips[ips->num-i].addr, &info);
2985 for (j=0; j < info->num; j++) {
2986 if (cifaces == NULL) {
2987 cifaces = talloc_strdup(info,
2988 info->ifaces[j].name);
2990 cifaces = talloc_asprintf_append(cifaces,
2992 info->ifaces[j].name);
2995 if (info->active_idx == j) {
2996 aciface = info->ifaces[j].name;
2999 if (info->ifaces[j].link_state == 0) {
3003 if (avifaces == NULL) {
3004 avifaces = talloc_strdup(info, info->ifaces[j].name);
3006 avifaces = talloc_asprintf_append(avifaces,
3008 info->ifaces[j].name);
3013 if (options.machinereadable){
3015 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3016 ips->ips[ips->num-i].pnn);
3017 if (options.verbose){
3020 avifaces?avifaces:"",
3021 cifaces?cifaces:"");
3025 if (options.verbose) {
3026 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3027 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3028 ips->ips[ips->num-i].pnn,
3030 avifaces?avifaces:"",
3031 cifaces?cifaces:"");
3034 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3035 ips->ips[ips->num-i].pnn);
3041 talloc_free(tmp_ctx);
3048 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3051 ctdb_sock_addr addr;
3052 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3053 struct ctdb_control_public_ip_info *info;
3056 talloc_free(tmp_ctx);
3060 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3061 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3065 /* read the public ip info from this node */
3066 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3067 tmp_ctx, &addr, &info);
3069 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3070 argv[0], options.pnn));
3071 talloc_free(tmp_ctx);
3075 printf("Public IP[%s] info on node %u\n",
3076 ctdb_addr_to_str(&info->ip.addr),
3079 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3080 ctdb_addr_to_str(&info->ip.addr),
3081 info->ip.pnn, info->num);
3083 for (i=0; i<info->num; i++) {
3084 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3086 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3087 i+1, info->ifaces[i].name,
3088 info->ifaces[i].link_state?"up":"down",
3089 (unsigned int)info->ifaces[i].references,
3090 (i==info->active_idx)?" (active)":"");
3093 talloc_free(tmp_ctx);
3098 display interfaces status
3100 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3102 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3104 struct ctdb_control_get_ifaces *ifaces;
3107 /* read the public ip list from this node */
3108 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3110 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3112 talloc_free(tmp_ctx);
3116 if (options.machinereadable){
3117 printf(":Name:LinkStatus:References:\n");
3119 printf("Interfaces on node %u\n", options.pnn);
3122 for (i=0; i<ifaces->num; i++) {
3123 if (options.machinereadable){
3124 printf(":%s:%s:%u\n",
3125 ifaces->ifaces[i].name,
3126 ifaces->ifaces[i].link_state?"1":"0",
3127 (unsigned int)ifaces->ifaces[i].references);
3129 printf("name:%s link:%s references:%u\n",
3130 ifaces->ifaces[i].name,
3131 ifaces->ifaces[i].link_state?"up":"down",
3132 (unsigned int)ifaces->ifaces[i].references);
3136 talloc_free(tmp_ctx);
3142 set link status of an interface
3144 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3147 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3148 struct ctdb_control_iface_info info;
3156 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3157 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3159 talloc_free(tmp_ctx);
3162 strcpy(info.name, argv[0]);
3164 if (strcmp(argv[1], "up") == 0) {
3165 info.link_state = 1;
3166 } else if (strcmp(argv[1], "down") == 0) {
3167 info.link_state = 0;
3169 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3171 talloc_free(tmp_ctx);
3175 /* read the public ip list from this node */
3176 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3179 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3180 argv[0], options.pnn));
3181 talloc_free(tmp_ctx);
3185 talloc_free(tmp_ctx);
3190 display pid of a ctdb daemon
3192 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3197 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3199 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3202 printf("Pid:%d\n", pid);
3207 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3209 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3211 update_flags_handler_t handler,
3216 struct ctdb_node_map *nodemap = NULL;
3220 /* Check if the node is already in the desired state */
3221 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3223 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3226 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3227 if (set_flag == flag_is_set) {
3228 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3229 (set_flag ? "already" : "not"), desc));
3234 if (!handler(ctdb, data)) {
3235 DEBUG(DEBUG_WARNING,
3236 ("Failed to send control to set state %s on node %u, try again\n",
3237 desc, options.pnn));
3242 /* Read the nodemap and verify the change took effect.
3243 * Even if the above control/hanlder timed out then it
3244 * could still have worked!
3246 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3249 DEBUG(DEBUG_WARNING,
3250 ("Unable to get nodemap from local node, try again\n"));
3252 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3253 } while (nodemap == NULL || (set_flag != flag_is_set));
3255 return ipreallocate(ctdb);
3258 /* Administratively disable a node */
3259 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3263 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3264 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3268 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3270 return update_flags_and_ipreallocate(ctdb, NULL,
3271 update_flags_disabled,
3272 NODE_FLAGS_PERMANENTLY_DISABLED,
3274 true /* set_flag*/);
3277 /* Administratively re-enable a node */
3278 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3282 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3283 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3287 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3289 return update_flags_and_ipreallocate(ctdb, NULL,
3290 update_flags_not_disabled,
3291 NODE_FLAGS_PERMANENTLY_DISABLED,
3293 false /* set_flag*/);
3297 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3301 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3306 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3308 return update_flags_and_ipreallocate(ctdb, NULL,
3309 update_flags_stopped,
3312 true /* set_flag*/);
3315 /* Continue a stopped node */
3316 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3320 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3325 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3327 return update_flags_and_ipreallocate(ctdb, NULL,
3328 update_flags_not_stopped,
3331 false /* set_flag */);
3334 static uint32_t get_generation(struct ctdb_context *ctdb)
3336 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3337 struct ctdb_vnn_map *vnnmap=NULL;
3339 uint32_t generation;
3341 /* wait until the recmaster is not in recovery mode */
3343 uint32_t recmode, recmaster;
3345 if (vnnmap != NULL) {
3346 talloc_free(vnnmap);
3350 /* get the recmaster */
3351 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3353 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3354 talloc_free(tmp_ctx);
3358 /* get recovery mode */
3359 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3361 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3362 talloc_free(tmp_ctx);
3366 /* get the current generation number */
3367 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3369 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3370 talloc_free(tmp_ctx);
3374 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3375 generation = vnnmap->generation;
3376 talloc_free(tmp_ctx);
3384 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3386 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3389 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3394 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3396 struct ctdb_ban_time bantime;
3402 bantime.pnn = options.pnn;
3403 bantime.time = strtoul(argv[0], NULL, 0);
3405 if (bantime.time == 0) {
3406 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3410 return update_flags_and_ipreallocate(ctdb, &bantime,
3411 update_state_banned,
3414 true /* set_flag*/);
3419 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3421 struct ctdb_ban_time bantime;
3423 bantime.pnn = options.pnn;
3426 return update_flags_and_ipreallocate(ctdb, &bantime,
3427 update_state_banned,
3430 false /* set_flag*/);
3434 show ban information for a node
3436 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3439 struct ctdb_node_map *nodemap=NULL;
3440 struct ctdb_ban_time *bantime;
3442 /* verify the node exists */
3443 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3445 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3449 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3451 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3455 if (bantime->time == 0) {
3456 printf("Node %u is not banned\n", bantime->pnn);
3458 printf("Node %u is banned, %d seconds remaining\n",
3459 bantime->pnn, bantime->time);
3468 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3472 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3474 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3484 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3487 uint32_t generation, next_generation;
3489 /* record the current generation number */
3490 generation = get_generation(ctdb);
3492 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3494 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3498 /* wait until we are in a new generation */
3500 next_generation = get_generation(ctdb);
3501 if (next_generation != generation) {
3512 display monitoring mode of a remote node
3514 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3519 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3521 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3524 if (!options.machinereadable){
3525 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3528 printf(":%d:\n",monmode);
3535 display capabilities of a remote node
3537 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3539 uint32_t capabilities;
3542 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3544 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3548 if (!options.machinereadable){
3549 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3550 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3551 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3552 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3554 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3555 printf(":%d:%d:%d:%d:\n",
3556 !!(capabilities&CTDB_CAP_RECMASTER),
3557 !!(capabilities&CTDB_CAP_LMASTER),
3558 !!(capabilities&CTDB_CAP_LVS),
3559 !!(capabilities&CTDB_CAP_NATGW));
3565 display lvs configuration
3568 static uint32_t lvs_exclude_flags[] = {
3569 /* Look for a nice healthy node */
3570 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3571 /* If not found, an UNHEALTHY node will do */
3572 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3576 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3578 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3579 struct ctdb_node_map *orig_nodemap=NULL;
3580 struct ctdb_node_map *nodemap;
3583 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3584 tmp_ctx, &orig_nodemap);
3586 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3587 talloc_free(tmp_ctx);
3591 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3592 CTDB_CAP_LVS, false);
3593 if (nodemap == NULL) {
3601 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3602 struct ctdb_node_map *t =
3603 filter_nodemap_by_flags(ctdb, nodemap,
3604 lvs_exclude_flags[i]);
3611 /* At least 1 node without excluded flags */
3613 for (j = 0; j < t->num; j++) {
3614 printf("%d:%s\n", t->nodes[j].pnn,
3615 ctdb_addr_to_str(&t->nodes[j].addr));
3622 talloc_free(tmp_ctx);
3627 display who is the lvs master
3629 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3631 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3632 struct ctdb_node_map *nodemap=NULL;
3635 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3638 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3639 talloc_free(tmp_ctx);
3643 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3644 struct ctdb_node_map *t =
3645 filter_nodemap_by_flags(ctdb, nodemap,
3646 lvs_exclude_flags[i]);
3653 struct ctdb_node_map *n;
3654 n = filter_nodemap_by_capabilities(ctdb,
3665 printf(options.machinereadable ?
3666 "%d\n" : "Node %d is LVS master\n",
3674 printf("There is no LVS master\n");
3677 talloc_free(tmp_ctx);
3682 disable monitoring on a node
3684 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3689 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3691 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3694 printf("Monitoring mode:%s\n","DISABLED");
3700 enable monitoring on a node
3702 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3707 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3709 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3712 printf("Monitoring mode:%s\n","ACTIVE");
3718 display remote list of keys/data for a db
3720 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3722 const char *db_name;
3723 struct ctdb_db_context *ctdb_db;
3725 struct ctdb_dump_db_context c;
3732 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3736 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3737 if (ctdb_db == NULL) {
3738 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3742 if (options.printlmaster) {
3743 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3744 ctdb, &ctdb->vnn_map);
3746 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3754 c.printemptyrecords = (bool)options.printemptyrecords;
3755 c.printdatasize = (bool)options.printdatasize;
3756 c.printlmaster = (bool)options.printlmaster;
3757 c.printhash = (bool)options.printhash;
3758 c.printrecordflags = (bool)options.printrecordflags;
3760 /* traverse and dump the cluster tdb */
3761 ret = ctdb_dump_db(ctdb_db, &c);
3763 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3764 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3765 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3769 talloc_free(ctdb_db);
3771 printf("Dumped %d records\n", ret);
3775 struct cattdb_data {
3776 struct ctdb_context *ctdb;
3780 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3782 struct cattdb_data *d = private_data;
3783 struct ctdb_dump_db_context c;
3789 c.printemptyrecords = (bool)options.printemptyrecords;
3790 c.printdatasize = (bool)options.printdatasize;
3791 c.printlmaster = false;
3792 c.printhash = (bool)options.printhash;
3793 c.printrecordflags = true;
3795 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3799 cat the local tdb database using same format as catdb
3801 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3803 const char *db_name;
3804 struct ctdb_db_context *ctdb_db;
3805 struct cattdb_data d;
3812 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3816 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3817 if (ctdb_db == NULL) {
3818 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3822 /* traverse the local tdb */
3825 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3826 printf("Failed to cattdb data\n");
3829 talloc_free(ctdb_db);
3831 printf("Dumped %d records\n", d.count);
3836 display the content of a database key
3838 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3840 const char *db_name;
3841 struct ctdb_db_context *ctdb_db;
3842 struct ctdb_record_handle *h;
3843 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3851 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3855 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3856 if (ctdb_db == NULL) {
3857 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3861 key.dptr = discard_const(argv[1]);
3862 key.dsize = strlen((char *)key.dptr);
3864 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3866 printf("Failed to fetch record '%s' on node %d\n",
3867 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3868 talloc_free(tmp_ctx);
3872 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3874 talloc_free(tmp_ctx);
3875 talloc_free(ctdb_db);
3880 display the content of a database key
3882 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3884 const char *db_name;
3885 struct ctdb_db_context *ctdb_db;
3886 struct ctdb_record_handle *h;
3887 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3895 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3899 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3900 if (ctdb_db == NULL) {
3901 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3905 key.dptr = discard_const(argv[1]);
3906 key.dsize = strlen((char *)key.dptr);
3908 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3910 printf("Failed to fetch record '%s' on node %d\n",
3911 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3912 talloc_free(tmp_ctx);
3916 data.dptr = discard_const(argv[2]);
3917 data.dsize = strlen((char *)data.dptr);
3919 if (ctdb_record_store(h, data) != 0) {
3920 printf("Failed to store record\n");
3924 talloc_free(tmp_ctx);
3925 talloc_free(ctdb_db);
3930 fetch a record from a persistent database
3932 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3934 const char *db_name;
3935 struct ctdb_db_context *ctdb_db;
3936 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3937 struct ctdb_transaction_handle *h;
3944 talloc_free(tmp_ctx);
3948 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3949 talloc_free(tmp_ctx);
3953 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3955 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3956 talloc_free(tmp_ctx);
3960 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3961 if (ctdb_db == NULL) {
3962 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3963 talloc_free(tmp_ctx);
3967 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3969 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3970 talloc_free(tmp_ctx);
3974 key.dptr = discard_const(argv[1]);
3975 key.dsize = strlen(argv[1]);
3976 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3978 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3979 talloc_free(tmp_ctx);
3983 if (data.dsize == 0 || data.dptr == NULL) {
3984 DEBUG(DEBUG_ERR,("Record is empty\n"));
3985 talloc_free(tmp_ctx);
3990 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3992 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3993 talloc_free(tmp_ctx);
3996 sys_write(fd, data.dptr, data.dsize);
3999 sys_write(1, data.dptr, data.dsize);
4002 /* abort the transaction */
4006 talloc_free(tmp_ctx);
4011 fetch a record from a tdb-file
4013 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4015 const char *tdb_file;
4018 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4027 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4029 printf("Failed to open TDB file %s\n", tdb_file);
4033 if (!strncmp(argv[1], "0x", 2)) {
4034 key = hextodata(tmp_ctx, argv[1] + 2);
4035 if (key.dsize == 0) {
4036 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4040 key.dptr = discard_const(argv[1]);
4041 key.dsize = strlen(argv[1]);
4044 data = tdb_fetch(tdb, key);
4045 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4046 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4054 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4056 printf("Failed to open output file %s\n", argv[2]);
4059 if (options.verbose){
4060 sys_write(fd, data.dptr, data.dsize);
4062 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4066 if (options.verbose){
4067 sys_write(1, data.dptr, data.dsize);
4069 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4073 talloc_free(tmp_ctx);
4078 store a record and header to a tdb-file
4080 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4082 const char *tdb_file;
4084 TDB_DATA key, value, data;
4085 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4086 struct ctdb_ltdb_header header;
4094 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4096 printf("Failed to open TDB file %s\n", tdb_file);
4100 if (!strncmp(argv[1], "0x", 2)) {
4101 key = hextodata(tmp_ctx, argv[1] + 2);
4102 if (key.dsize == 0) {
4103 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4107 key.dptr = discard_const(argv[1]);
4108 key.dsize = strlen(argv[1]);
4111 if (!strncmp(argv[2], "0x", 2)) {
4112 value = hextodata(tmp_ctx, argv[2] + 2);
4113 if (value.dsize == 0) {
4114 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4118 value.dptr = discard_const(argv[2]);
4119 value.dsize = strlen(argv[2]);
4122 ZERO_STRUCT(header);
4124 header.rsn = atoll(argv[3]);
4127 header.dmaster = atoi(argv[4]);
4130 header.flags = atoi(argv[5]);
4133 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4134 data.dptr = talloc_size(tmp_ctx, data.dsize);
4135 if (data.dptr == NULL) {
4136 printf("Failed to allocate header+value\n");
4140 *(struct ctdb_ltdb_header *)data.dptr = header;
4141 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4143 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4144 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4151 talloc_free(tmp_ctx);
4156 write a record to a persistent database
4158 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4160 const char *db_name;
4161 struct ctdb_db_context *ctdb_db;
4162 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4163 struct ctdb_transaction_handle *h;
4169 talloc_free(tmp_ctx);
4173 fd = open(argv[2], O_RDONLY);
4175 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4176 talloc_free(tmp_ctx);
4180 ret = fstat(fd, &st);
4182 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4184 talloc_free(tmp_ctx);
4188 if (!S_ISREG(st.st_mode)) {
4189 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4191 talloc_free(tmp_ctx);
4195 data.dsize = st.st_size;
4196 if (data.dsize == 0) {
4199 data.dptr = talloc_size(tmp_ctx, data.dsize);
4200 if (data.dptr == NULL) {
4201 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4203 talloc_free(tmp_ctx);
4206 ret = sys_read(fd, data.dptr, data.dsize);
4207 if (ret != data.dsize) {
4208 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4210 talloc_free(tmp_ctx);
4219 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4220 if (ctdb_db == NULL) {
4221 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4222 talloc_free(tmp_ctx);
4226 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4228 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4229 talloc_free(tmp_ctx);
4233 key.dptr = discard_const(argv[1]);
4234 key.dsize = strlen(argv[1]);
4235 ret = ctdb_transaction_store(h, key, data);
4237 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4238 talloc_free(tmp_ctx);
4242 ret = ctdb_transaction_commit(h);
4244 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4245 talloc_free(tmp_ctx);
4250 talloc_free(tmp_ctx);
4255 * delete a record from a persistent database
4257 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4259 const char *db_name;
4260 struct ctdb_db_context *ctdb_db;
4261 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4262 struct ctdb_transaction_handle *h;
4269 talloc_free(tmp_ctx);
4273 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4274 talloc_free(tmp_ctx);
4278 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4280 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4281 talloc_free(tmp_ctx);
4285 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4286 if (ctdb_db == NULL) {
4287 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4288 talloc_free(tmp_ctx);
4292 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4294 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4295 talloc_free(tmp_ctx);
4299 key.dptr = discard_const(argv[1]);
4300 key.dsize = strlen(argv[1]);
4301 ret = ctdb_transaction_store(h, key, tdb_null);
4303 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4304 talloc_free(tmp_ctx);
4308 ret = ctdb_transaction_commit(h);
4310 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4311 talloc_free(tmp_ctx);
4315 talloc_free(tmp_ctx);
4319 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4324 const char *ret; /* Next byte after successfully parsed value */
4326 /* Error, unless someone says otherwise */
4328 /* Indicates no value to parse */
4331 /* Skip whitespace */
4332 n = strspn(s, " \t");
4336 /* Quoted ASCII string - no wide characters! */
4338 n = strcspn(t, "\"");
4342 data->dptr = talloc_memdup(mem_ctx, t, n);
4343 CTDB_NOMEM_ABORT(data->dptr);
4347 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4350 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4356 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4357 TDB_DATA *key, TDB_DATA *value)
4359 char line [1024]; /* FIXME: make this more flexible? */
4363 ptr = fgets(line, sizeof(line), file);
4370 t = ptrans_parse_string(mem_ctx, line, key);
4371 if (t == NULL || key->dptr == NULL) {
4372 /* Line Ignored but not EOF */
4377 t = ptrans_parse_string(mem_ctx, t, value);
4379 /* Line Ignored but not EOF */
4380 talloc_free(key->dptr);
4389 * Update a persistent database as per file/stdin
4391 static int control_ptrans(struct ctdb_context *ctdb,
4392 int argc, const char **argv)
4394 const char *db_name;
4395 struct ctdb_db_context *ctdb_db;
4396 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4397 struct ctdb_transaction_handle *h;
4398 TDB_DATA key, value;
4403 talloc_free(tmp_ctx);
4409 file = fopen(argv[1], "r");
4411 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4412 talloc_free(tmp_ctx);
4419 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4420 if (ctdb_db == NULL) {
4421 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4425 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4427 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4431 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4432 if (key.dsize != 0) {
4433 ret = ctdb_transaction_store(h, key, value);
4434 /* Minimise memory use */
4435 talloc_free(key.dptr);
4436 if (value.dptr != NULL) {
4437 talloc_free(value.dptr);
4440 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4441 ctdb_transaction_cancel(h);
4447 ret = ctdb_transaction_commit(h);
4449 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4453 if (file != stdin) {
4456 talloc_free(tmp_ctx);
4460 if (file != stdin) {
4464 talloc_free(tmp_ctx);
4469 check if a service is bound to a port or not
4471 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4476 struct sockaddr_in sin;
4479 printf("Use: ctdb chktcport <port>\n");
4483 port = atoi(argv[0]);
4485 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4487 printf("Failed to open local socket\n");
4491 v = fcntl(s, F_GETFL, 0);
4492 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4493 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4496 bzero(&sin, sizeof(sin));
4497 sin.sin_family = PF_INET;
4498 sin.sin_port = htons(port);
4499 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4502 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4510 /* Reload public IPs on a specified nodes */
4511 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4513 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4519 assert_single_node_only();
4525 /* Determine the nodes where IPs need to be reloaded */
4526 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4527 options.pnn, true, &nodes, &pnn_mode)) {
4533 /* Disable takeover runs on all connected nodes. A reply
4534 * indicating success is needed from each node so all nodes
4535 * will need to be active. This will retry until maxruntime
4536 * is exceeded, hence no error handling.
4538 * A check could be added to not allow reloading of IPs when
4539 * there are disconnected nodes. However, this should
4540 * probably be left up to the administrator.
4542 timeout = LONGTIMEOUT;
4543 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4544 "Disable takeover runs", true);
4546 /* Now tell all the desired nodes to reload their public IPs.
4547 * Keep trying this until it succeeds. This assumes all
4548 * failures are transient, which might not be true...
4550 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4551 nodes, 0, LONGTIMELIMIT(),
4553 NULL, NULL, NULL) != 0) {
4555 ("Unable to reload IPs on some nodes, try again.\n"));
4559 /* It isn't strictly necessary to wait until takeover runs are
4560 * re-enabled but doing so can't hurt.
4563 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4564 "Enable takeover runs", true);
4570 talloc_free(tmp_ctx);
4575 display a list of the databases on a remote ctdb
4577 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4580 struct ctdb_dbid_map *dbmap=NULL;
4582 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4584 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4588 if(options.machinereadable){
4589 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4590 for(i=0;i<dbmap->num;i++){
4598 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4599 dbmap->dbs[i].dbid, ctdb, &path);
4600 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4601 dbmap->dbs[i].dbid, ctdb, &name);
4602 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4603 dbmap->dbs[i].dbid, ctdb, &health);
4604 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4605 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4606 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4607 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4608 dbmap->dbs[i].dbid, name, path,
4609 !!(persistent), !!(sticky),
4610 !!(health), !!(readonly));
4615 printf("Number of databases:%d\n", dbmap->num);
4616 for(i=0;i<dbmap->num;i++){
4624 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4625 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4626 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4627 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4628 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4629 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4630 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4631 dbmap->dbs[i].dbid, name, path,
4632 persistent?" PERSISTENT":"",
4633 sticky?" STICKY":"",
4634 readonly?" READONLY":"",
4635 health?" UNHEALTHY":"");
4642 display the status of a database on a remote ctdb
4644 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4646 const char *db_name;
4656 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4660 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4661 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4662 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4663 db_id, db_name, path,
4664 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4665 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4666 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4667 (health ? health : "OK"));
4673 check if the local node is recmaster or not
4674 it will return 1 if this node is the recmaster and 0 if it is not
4675 or if the local ctdb daemon could not be contacted
4677 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4679 uint32_t mypnn, recmaster;
4682 assert_single_node_only();
4684 mypnn = getpnn(ctdb);
4686 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4688 printf("Failed to get the recmaster\n");
4692 if (recmaster != mypnn) {
4693 printf("this node is not the recmaster\n");
4697 printf("this node is the recmaster\n");
4704 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4707 struct timeval tv = timeval_current();
4708 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4710 printf("Unable to get ping response from node %u\n", options.pnn);
4713 printf("response from %u time=%.6f sec (%d clients)\n",
4714 options.pnn, timeval_elapsed(&tv), ret);
4721 get a node's runstate
4723 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4726 enum ctdb_runstate runstate;
4728 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4730 printf("Unable to get runstate response from node %u\n",
4735 enum ctdb_runstate t;
4737 for (i=0; i<argc; i++) {
4739 t = runstate_from_string(argv[i]);
4740 if (t == CTDB_RUNSTATE_UNKNOWN) {
4741 printf("Invalid run state (%s)\n", argv[i]);
4745 if (t == runstate) {
4752 printf("CTDB not in required run state (got %s)\n",
4753 runstate_to_string((enum ctdb_runstate)runstate));
4758 printf("%s\n", runstate_to_string(runstate));
4766 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4777 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4779 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4783 printf("%-23s = %u\n", name, value);
4790 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4801 value = strtoul(argv[1], NULL, 0);
4803 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4805 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4814 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4820 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4822 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4826 for (i=0;i<count;i++) {
4827 control_getvar(ctdb, 1, &list[i]);
4836 display debug level on a node
4838 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4843 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4845 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4848 if (options.machinereadable){
4849 printf(":Name:Level:\n");
4850 printf(":%s:%d:\n",get_debug_by_level(level),level);
4852 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4859 display reclock file of a node
4861 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4864 const char *reclock;
4866 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4868 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4871 if (options.machinereadable){
4872 if (reclock != NULL) {
4873 printf("%s", reclock);
4876 if (reclock == NULL) {
4877 printf("No reclock file used.\n");
4879 printf("Reclock file:%s\n", reclock);
4887 set the reclock file of a node
4889 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4892 const char *reclock;
4896 } else if (argc == 1) {
4902 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4904 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4911 set the natgw state on/off
4913 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4916 uint32_t natgwstate;
4922 if (!strcmp(argv[0], "on")) {
4924 } else if (!strcmp(argv[0], "off")) {
4930 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4932 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4940 set the lmaster role on/off
4942 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4945 uint32_t lmasterrole;
4951 if (!strcmp(argv[0], "on")) {
4953 } else if (!strcmp(argv[0], "off")) {
4959 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4961 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4969 set the recmaster role on/off
4971 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4974 uint32_t recmasterrole;
4980 if (!strcmp(argv[0], "on")) {
4982 } else if (!strcmp(argv[0], "off")) {
4988 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4990 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4998 set debug level on a node or all nodes
5000 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5006 printf("You must specify the debug level. Valid levels are:\n");
5007 for (i=0; debug_levels[i].description != NULL; i++) {
5008 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5014 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
5015 level = get_debug_by_desc(argv[0]);
5017 level = strtol(argv[0], NULL, 0);
5020 for (i=0; debug_levels[i].description != NULL; i++) {
5021 if (level == debug_levels[i].level) {
5025 if (debug_levels[i].description == NULL) {
5026 printf("Invalid debug level, must be one of\n");
5027 for (i=0; debug_levels[i].description != NULL; i++) {
5028 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5033 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5035 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5044 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5050 priority = strtol(argv[0], NULL, 0);
5054 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5056 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5058 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5065 attach to a database
5067 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5069 const char *db_name;
5070 struct ctdb_db_context *ctdb_db;
5071 bool persistent = false;
5081 if (strcmp(argv[1], "persistent") != 0) {
5087 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5088 if (ctdb_db == NULL) {
5089 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5097 * detach from a database
5099 static int control_detach(struct ctdb_context *ctdb, int argc,
5104 int ret, i, status = 0;
5105 struct ctdb_node_map *nodemap = NULL;
5106 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5113 assert_single_node_only();
5115 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5118 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5119 "when recovery is active\n"));
5120 talloc_free(tmp_ctx);
5124 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5127 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5129 talloc_free(tmp_ctx);
5133 for (i=0; i<nodemap->num; i++) {
5136 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5140 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5144 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5145 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5146 "inactive (stopped or banned) node "
5147 "%u\n", nodemap->nodes[i].pnn));
5148 talloc_free(tmp_ctx);
5152 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5153 nodemap->nodes[i].pnn,
5154 "AllowClientDBAttach",
5157 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5158 "AllowClientDBAttach from node %u\n",
5159 nodemap->nodes[i].pnn));
5160 talloc_free(tmp_ctx);
5165 DEBUG(DEBUG_ERR, ("Database access is still active on "
5166 "node %u. Set AllowClientDBAttach=0 "
5168 nodemap->nodes[i].pnn));
5169 talloc_free(tmp_ctx);
5174 talloc_free(tmp_ctx);
5176 for (i=0; i<argc; i++) {
5177 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5181 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5182 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5183 "cannot be detached\n", argv[i]));
5188 ret = ctdb_detach(ctdb, db_id);
5190 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5202 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5204 struct ctdb_db_priority db_prio;
5211 db_prio.db_id = strtoul(argv[0], NULL, 0);
5212 db_prio.priority = strtoul(argv[1], NULL, 0);
5214 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5216 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5226 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5228 uint32_t db_id, priority;
5235 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5239 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5241 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5245 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5251 set the sticky records capability for a database
5253 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5255 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5263 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5267 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5269 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5270 talloc_free(tmp_ctx);
5274 talloc_free(tmp_ctx);
5279 set the readonly capability for a database
5281 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5283 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5291 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5295 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5297 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5298 talloc_free(tmp_ctx);
5302 talloc_free(tmp_ctx);
5309 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5319 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5323 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5325 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5329 printf("Sequence number:%lld\n", (long long)seqnum);
5335 run an eventscript on a node
5337 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5343 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5346 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5350 data.dptr = (unsigned char *)discard_const(argv[0]);
5351 data.dsize = strlen((char *)data.dptr) + 1;
5353 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5355 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5356 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5357 if (ret != 0 || res != 0) {
5358 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5359 talloc_free(tmp_ctx);
5362 talloc_free(tmp_ctx);
5366 #define DB_VERSION 1
5367 #define MAX_DB_NAME 64
5368 struct db_file_header {
5369 unsigned long version;
5371 unsigned long persistent;
5373 const char name[MAX_DB_NAME];
5376 struct backup_data {
5377 struct ctdb_marshall_buffer *records;
5380 bool traverse_error;
5383 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5385 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5386 struct ctdb_rec_data *rec;
5388 /* add the record */
5389 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5391 bd->traverse_error = true;
5392 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5395 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5396 if (bd->records == NULL) {
5397 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5398 bd->traverse_error = true;
5401 bd->records->count++;
5402 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5403 bd->len += rec->length;
5411 * backup a database to a file
5413 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5415 const char *db_name;
5417 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5418 struct db_file_header dbhdr;
5419 struct ctdb_db_context *ctdb_db;
5420 struct backup_data *bd;
5423 const char *reason = NULL;
5427 assert_single_node_only();
5430 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5434 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5438 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5439 db_id, tmp_ctx, &reason);
5441 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5443 talloc_free(tmp_ctx);
5447 uint32_t allow_unhealthy = 0;
5449 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5450 "AllowUnhealthyDBRead",
5453 if (allow_unhealthy != 1) {
5454 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5457 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5459 talloc_free(tmp_ctx);
5463 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5465 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5466 "tunnable AllowUnhealthyDBRead = %u\n",
5470 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5471 if (ctdb_db == NULL) {
5472 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5473 talloc_free(tmp_ctx);
5478 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5480 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5481 talloc_free(tmp_ctx);
5486 bd = talloc_zero(tmp_ctx, struct backup_data);
5488 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5489 talloc_free(tmp_ctx);
5493 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5494 if (bd->records == NULL) {
5495 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5496 talloc_free(tmp_ctx);
5500 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5501 bd->records->db_id = ctdb_db->db_id;
5502 /* traverse the database collecting all records */
5503 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5504 bd->traverse_error) {
5505 DEBUG(DEBUG_ERR,("Traverse error\n"));
5506 talloc_free(tmp_ctx);
5510 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5513 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5515 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5516 talloc_free(tmp_ctx);
5521 dbhdr.version = DB_VERSION;
5522 dbhdr.timestamp = time(NULL);
5523 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5524 dbhdr.size = bd->len;
5525 if (strlen(argv[0]) >= MAX_DB_NAME) {
5526 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5529 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5530 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
5532 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5535 ret = sys_write(fh, bd->records, bd->len);
5537 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5546 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5550 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5552 talloc_free(tmp_ctx);
5557 * restore a database from a file
5559 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5562 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5565 struct db_file_header dbhdr;
5566 struct ctdb_db_context *ctdb_db;
5567 struct ctdb_node_map *nodemap=NULL;
5568 struct ctdb_vnn_map *vnnmap=NULL;
5570 struct ctdb_control_wipe_database w;
5572 uint32_t generation;
5577 assert_single_node_only();
5579 if (argc < 1 || argc > 2) {
5580 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5584 fh = open(argv[0], O_RDONLY);
5586 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5587 talloc_free(tmp_ctx);
5591 sys_read(fh, &dbhdr, sizeof(dbhdr));
5592 if (dbhdr.version != DB_VERSION) {
5593 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5595 talloc_free(tmp_ctx);
5599 dbname = discard_const(dbhdr.name);
5601 dbname = discard_const(argv[1]);
5604 outdata.dsize = dbhdr.size;
5605 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5606 if (outdata.dptr == NULL) {
5607 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5609 talloc_free(tmp_ctx);
5612 sys_read(fh, outdata.dptr, outdata.dsize);
5615 tm = localtime(&dbhdr.timestamp);
5616 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5617 printf("Restoring database '%s' from backup @ %s\n",
5621 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5622 if (ctdb_db == NULL) {
5623 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5624 talloc_free(tmp_ctx);
5628 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5630 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5631 talloc_free(tmp_ctx);
5636 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5638 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5639 talloc_free(tmp_ctx);
5643 /* freeze all nodes */
5644 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5645 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5646 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5652 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5653 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5654 talloc_free(tmp_ctx);
5659 generation = vnnmap->generation;
5660 data.dptr = (void *)&generation;
5661 data.dsize = sizeof(generation);
5663 /* start a cluster wide transaction */
5664 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5665 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5667 TIMELIMIT(), false, data,
5670 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5675 w.db_id = ctdb_db->db_id;
5676 w.transaction_id = generation;
5678 data.dptr = (void *)&w;
5679 data.dsize = sizeof(w);
5681 /* wipe all the remote databases. */
5682 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5683 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5685 TIMELIMIT(), false, data,
5688 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5689 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5690 talloc_free(tmp_ctx);
5694 /* push the database */
5695 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5696 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5698 TIMELIMIT(), false, outdata,
5701 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5702 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5703 talloc_free(tmp_ctx);
5707 data.dptr = (void *)&ctdb_db->db_id;
5708 data.dsize = sizeof(ctdb_db->db_id);
5710 /* mark the database as healthy */
5711 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5712 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5714 TIMELIMIT(), false, data,
5717 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5718 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5719 talloc_free(tmp_ctx);
5723 data.dptr = (void *)&generation;
5724 data.dsize = sizeof(generation);
5726 /* commit all the changes */
5727 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5729 TIMELIMIT(), false, data,
5732 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5733 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5734 talloc_free(tmp_ctx);
5739 /* thaw all nodes */
5740 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5741 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5747 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5748 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5749 talloc_free(tmp_ctx);
5754 talloc_free(tmp_ctx);
5759 * dump a database backup from a file
5761 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5763 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5765 struct db_file_header dbhdr;
5769 struct ctdb_rec_data *rec = NULL;
5770 struct ctdb_marshall_buffer *m;
5771 struct ctdb_dump_db_context c;
5773 assert_single_node_only();
5776 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5780 fh = open(argv[0], O_RDONLY);
5782 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5783 talloc_free(tmp_ctx);
5787 sys_read(fh, &dbhdr, sizeof(dbhdr));
5788 if (dbhdr.version != DB_VERSION) {
5789 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5791 talloc_free(tmp_ctx);
5795 outdata.dsize = dbhdr.size;
5796 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5797 if (outdata.dptr == NULL) {
5798 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5800 talloc_free(tmp_ctx);
5803 sys_read(fh, outdata.dptr, outdata.dsize);
5805 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5807 tm = localtime(&dbhdr.timestamp);
5808 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5809 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5810 dbhdr.name, m->db_id, tbuf);
5814 c.printemptyrecords = (bool)options.printemptyrecords;
5815 c.printdatasize = (bool)options.printdatasize;
5816 c.printlmaster = false;
5817 c.printhash = (bool)options.printhash;
5818 c.printrecordflags = (bool)options.printrecordflags;
5820 for (i=0; i < m->count; i++) {
5824 /* we do not want the header splitted, so we pass NULL*/
5825 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5828 ctdb_dumpdb_record(ctdb, key, data, &c);
5831 printf("Dumped %d records\n", i);
5832 talloc_free(tmp_ctx);
5837 * wipe a database from a file
5839 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5842 const char *db_name;
5844 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5846 struct ctdb_db_context *ctdb_db;
5847 struct ctdb_node_map *nodemap = NULL;
5848 struct ctdb_vnn_map *vnnmap = NULL;
5850 struct ctdb_control_wipe_database w;
5852 uint32_t generation;
5855 assert_single_node_only();
5858 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5862 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5866 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5867 if (ctdb_db == NULL) {
5868 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5870 talloc_free(tmp_ctx);
5874 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5877 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5879 talloc_free(tmp_ctx);
5883 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5886 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5888 talloc_free(tmp_ctx);
5892 /* freeze all nodes */
5893 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5894 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5895 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5902 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5903 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5904 CTDB_RECOVERY_ACTIVE);
5905 talloc_free(tmp_ctx);
5910 generation = vnnmap->generation;
5911 data.dptr = (void *)&generation;
5912 data.dsize = sizeof(generation);
5914 /* start a cluster wide transaction */
5915 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5916 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5918 TIMELIMIT(), false, data,
5922 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5923 "transactions.\n"));
5927 w.db_id = ctdb_db->db_id;
5928 w.transaction_id = generation;
5930 data.dptr = (void *)&w;
5931 data.dsize = sizeof(w);
5933 /* wipe all the remote databases. */
5934 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5935 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5937 TIMELIMIT(), false, data,
5940 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5941 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5942 talloc_free(tmp_ctx);
5946 data.dptr = (void *)&ctdb_db->db_id;
5947 data.dsize = sizeof(ctdb_db->db_id);
5949 /* mark the database as healthy */
5950 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5951 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5953 TIMELIMIT(), false, data,
5956 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5957 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5958 talloc_free(tmp_ctx);
5962 data.dptr = (void *)&generation;
5963 data.dsize = sizeof(generation);
5965 /* commit all the changes */
5966 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5968 TIMELIMIT(), false, data,
5971 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5972 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5973 talloc_free(tmp_ctx);
5977 /* thaw all nodes */
5978 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5979 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5985 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5986 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5987 talloc_free(tmp_ctx);
5991 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5993 talloc_free(tmp_ctx);
6000 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6006 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6007 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6008 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6009 if (ret != 0 || res != 0) {
6010 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6011 talloc_free(tmp_ctx);
6014 sys_write(1, data.dptr, data.dsize);
6015 talloc_free(tmp_ctx);
6020 handler for memory dumps
6022 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6023 TDB_DATA data, void *private_data)
6025 sys_write(1, data.dptr, data.dsize);
6030 dump memory usage on the recovery daemon
6032 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6036 struct srvid_request rd;
6038 rd.pnn = ctdb_get_pnn(ctdb);
6039 rd.srvid = getpid();
6041 /* register a message port for receiveing the reply so that we
6042 can receive the reply
6044 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6047 data.dptr = (uint8_t *)&rd;
6048 data.dsize = sizeof(rd);
6050 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6052 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6056 /* this loop will terminate when we have received the reply */
6058 event_loop_once(ctdb->ev);
6065 send a message to a srvid
6067 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6069 unsigned long srvid;
6077 srvid = strtoul(argv[0], NULL, 0);
6079 data.dptr = (uint8_t *)discard_const(argv[1]);
6080 data.dsize= strlen(argv[1]);
6082 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6084 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6092 handler for msglisten
6094 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6095 TDB_DATA data, void *private_data)
6099 printf("Message received: ");
6100 for (i=0;i<data.dsize;i++) {
6101 printf("%c", data.dptr[i]);
6107 listen for messages on a messageport
6109 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6115 /* register a message port and listen for messages
6117 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6118 printf("Listening for messages on srvid:%d\n", (int)srvid);
6121 event_loop_once(ctdb->ev);
6128 list all nodes in the cluster
6129 we parse the nodes file directly
6131 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6133 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6134 struct pnn_node *pnn_nodes;
6135 struct pnn_node *pnn_node;
6137 assert_single_node_only();
6139 pnn_nodes = read_nodes_file(mem_ctx);
6140 if (pnn_nodes == NULL) {
6141 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6142 talloc_free(mem_ctx);
6146 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6147 const char *addr = ctdb_addr_to_str(&pnn_node->addr);
6148 if (options.machinereadable){
6149 printf(":%d:%s:\n", pnn_node->pnn, addr);
6151 printf("%s\n", addr);
6154 talloc_free(mem_ctx);
6160 reload the nodes file on the local node
6162 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6166 struct ctdb_node_map *nodemap=NULL;
6168 assert_single_node_only();
6170 mypnn = ctdb_get_pnn(ctdb);
6172 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6174 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6178 /* reload the nodes file on all remote nodes */
6179 for (i=0;i<nodemap->num;i++) {
6180 if (nodemap->nodes[i].pnn == mypnn) {
6183 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6184 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6185 nodemap->nodes[i].pnn);
6187 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6191 /* reload the nodes file on the local node */
6192 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6193 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6195 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6198 /* initiate a recovery */
6199 control_recover(ctdb, argc, argv);
6205 static const struct {
6207 int (*fn)(struct ctdb_context *, int, const char **);
6209 bool without_daemon; /* can be run without daemon running ? */
6212 } ctdb_commands[] = {
6213 { "version", control_version, true, true, "show version of ctdb" },
6214 { "status", control_status, true, false, "show node status" },
6215 { "uptime", control_uptime, true, false, "show node uptime" },
6216 { "ping", control_ping, true, false, "ping all nodes" },
6217 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6218 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6219 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6220 { "listvars", control_listvars, true, false, "list tunable variables"},
6221 { "statistics", control_statistics, false, false, "show statistics" },
6222 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6223 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6224 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6225 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6226 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6227 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6228 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6229 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6230 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6231 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6232 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6233 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6234 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6235 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6236 { "lvs", control_lvs, true, false, "show lvs configuration" },
6237 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6238 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6239 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6240 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6241 { "getdebug", control_getdebug, true, false, "get debug level" },
6242 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6243 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6244 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6245 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6246 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6247 { "disable", control_disable, true, false, "disable a nodes public IP" },
6248 { "enable", control_enable, true, false, "enable a nodes public IP" },
6249 { "stop", control_stop, true, false, "stop a node" },
6250 { "continue", control_continue, true, false, "re-start a stopped node" },
6251 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6252 { "unban", control_unban, true, false, "unban a node" },
6253 { "showban", control_showban, true, false, "show ban information"},
6254 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6255 { "recover", control_recover, true, false, "force recovery" },
6256 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6257 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6258 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6259 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6260 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6261 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6262 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6263 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6264 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6266 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6268 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6269 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6270 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6271 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6272 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6273 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6274 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6275 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6276 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6277 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6278 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6279 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6280 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6281 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6282 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6283 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6284 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6285 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6286 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6287 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6288 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6289 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6290 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6291 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6292 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6293 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6294 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6295 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6296 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6297 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6298 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6299 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6300 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6301 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6302 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6303 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6304 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6305 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6306 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6307 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6308 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6309 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6310 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6311 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6312 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6313 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6314 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6315 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6316 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6322 static void usage(void)
6326 "Usage: ctdb [options] <control>\n" \
6328 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6329 " -Y generate machinereadable output\n"
6330 " -v generate verbose output\n"
6331 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6332 printf("Controls:\n");
6333 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6334 printf(" %-15s %-27s %s\n",
6335 ctdb_commands[i].name,
6336 ctdb_commands[i].args?ctdb_commands[i].args:"",
6337 ctdb_commands[i].msg);
6343 static void ctdb_alarm(int sig)
6345 printf("Maximum runtime exceeded - exiting\n");
6352 int main(int argc, const char *argv[])
6354 struct ctdb_context *ctdb;
6355 char *nodestring = NULL;
6356 struct poptOption popt_options[] = {
6359 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6360 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6361 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6362 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6363 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6364 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6365 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6366 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6367 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6368 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6372 const char **extra_argv;
6376 struct event_context *ev;
6377 const char *control;
6381 /* set some defaults */
6382 options.maxruntime = 0;
6383 options.timelimit = 10;
6384 options.pnn = CTDB_CURRENT_NODE;
6386 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6388 while ((opt = poptGetNextOpt(pc)) != -1) {
6391 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6392 poptBadOption(pc, 0), poptStrerror(opt)));
6397 /* setup the remaining options for the main program to use */
6398 extra_argv = poptGetArgs(pc);
6401 while (extra_argv[extra_argc]) extra_argc++;
6404 if (extra_argc < 1) {
6408 if (options.maxruntime == 0) {
6409 const char *ctdb_timeout;
6410 ctdb_timeout = getenv("CTDB_TIMEOUT");
6411 if (ctdb_timeout != NULL) {
6412 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6414 /* default timeout is 120 seconds */
6415 options.maxruntime = 120;
6419 signal(SIGALRM, ctdb_alarm);
6420 alarm(options.maxruntime);
6422 control = extra_argv[0];
6424 /* Default value for CTDB_BASE - don't override */
6425 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6427 ev = event_context_init(NULL);
6429 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6433 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6434 if (strcmp(control, ctdb_commands[i].name) == 0) {
6439 if (i == ARRAY_SIZE(ctdb_commands)) {
6440 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6444 if (ctdb_commands[i].without_daemon == true) {
6445 if (nodestring != NULL) {
6446 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6449 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6452 /* initialise ctdb */
6453 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6457 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6459 pnn = find_node_xpnn();
6462 ("Is this node part of a CTDB cluster?\n"));
6467 /* setup the node number(s) to contact */
6468 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6469 &options.nodes, &options.pnn)) {
6473 if (options.pnn == CTDB_CURRENT_NODE) {
6474 options.pnn = options.nodes[0];
6477 if (ctdb_commands[i].auto_all &&
6478 ((options.pnn == CTDB_BROADCAST_ALL) ||
6479 (options.pnn == CTDB_MULTICAST))) {
6483 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6484 options.pnn = options.nodes[j];
6485 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6488 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6493 (void)poptFreeContext(pc);