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"
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 int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
62 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
66 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
67 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
68 "Out of memory in " __location__ )); \
72 static uint32_t getpnn(struct ctdb_context *ctdb)
74 if ((options.pnn == CTDB_BROADCAST_ALL) ||
75 (options.pnn == CTDB_MULTICAST)) {
77 ("Cannot get PNN for node %u\n", options.pnn));
81 if (options.pnn == CTDB_CURRENT_NODE) {
82 return ctdb_get_pnn(ctdb);
88 static void assert_single_node_only(void)
90 if ((options.pnn == CTDB_BROADCAST_ALL) ||
91 (options.pnn == CTDB_MULTICAST)) {
93 ("This control can not be applied to multiple PNNs\n"));
98 /* Pretty print the flags to a static buffer in human-readable format.
99 * This never returns NULL!
101 static const char *pretty_print_flags(uint32_t flags)
104 static const struct {
108 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
109 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
110 { NODE_FLAGS_BANNED, "BANNED" },
111 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
112 { NODE_FLAGS_DELETED, "DELETED" },
113 { NODE_FLAGS_STOPPED, "STOPPED" },
114 { NODE_FLAGS_INACTIVE, "INACTIVE" },
116 static char flags_str[512]; /* Big enough to contain all flag names */
119 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
120 if (flags & flag_names[j].flag) {
121 if (flags_str[0] == '\0') {
122 (void) strcpy(flags_str, flag_names[j].name);
124 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
125 (void) strncat(flags_str, flag_names[j].name,
126 sizeof(flags_str)-1);
130 if (flags_str[0] == '\0') {
131 (void) strcpy(flags_str, "OK");
137 static int h2i(char h)
139 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
140 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
144 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
147 TDB_DATA key = {NULL, 0};
151 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
156 key.dptr = talloc_size(mem_ctx, key.dsize);
158 for (i=0; i < len/2; i++) {
159 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
164 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
165 * that are disconnected or deleted. If dd_ok is true those nodes are
166 * included in the output list of nodes. If dd_ok is false, those
167 * nodes are filtered from the "all" case and cause an error if
168 * explicitly specified.
170 static bool parse_nodestring(struct ctdb_context *ctdb,
172 const char * nodestring,
173 uint32_t current_pnn,
178 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
181 struct ctdb_node_map *nodemap;
186 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
188 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
189 talloc_free(tmp_ctx);
193 if (nodestring != NULL) {
194 *nodes = talloc_array(mem_ctx, uint32_t, 0);
195 if (*nodes == NULL) {
201 if (strcmp(nodestring, "all") == 0) {
202 *pnn_mode = CTDB_BROADCAST_ALL;
205 for (i = 0; i < nodemap->num; i++) {
206 if ((nodemap->nodes[i].flags &
207 (NODE_FLAGS_DISCONNECTED |
208 NODE_FLAGS_DELETED)) && !dd_ok) {
211 *nodes = talloc_realloc(mem_ctx, *nodes,
213 if (*nodes == NULL) {
223 ns = talloc_strdup(tmp_ctx, nodestring);
224 tok = strtok(ns, ",");
225 while (tok != NULL) {
228 i = (uint32_t)strtoul(tok, &endptr, 0);
229 if (i == 0 && tok == endptr) {
231 ("Invalid node %s\n", tok));
232 talloc_free(tmp_ctx);
235 if (i >= nodemap->num) {
236 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
237 talloc_free(tmp_ctx);
240 if ((nodemap->nodes[i].flags &
241 (NODE_FLAGS_DISCONNECTED |
242 NODE_FLAGS_DELETED)) && !dd_ok) {
243 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
244 talloc_free(tmp_ctx);
247 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
248 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
249 talloc_free(tmp_ctx);
253 *nodes = talloc_realloc(mem_ctx, *nodes,
255 if (*nodes == NULL) {
262 tok = strtok(NULL, ",");
267 *pnn_mode = (*nodes)[0];
269 *pnn_mode = CTDB_MULTICAST;
273 /* default - no nodes specified */
274 *nodes = talloc_array(mem_ctx, uint32_t, 1);
275 if (*nodes == NULL) {
278 *pnn_mode = CTDB_CURRENT_NODE;
280 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
285 talloc_free(tmp_ctx);
289 talloc_free(tmp_ctx);
294 check if a database exists
296 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
297 uint32_t *dbid, const char **dbname, uint8_t *flags)
300 struct ctdb_dbid_map *dbmap=NULL;
301 bool dbid_given = false, found = false;
303 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
306 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
308 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
312 if (strncmp(dbarg, "0x", 2) == 0) {
313 id = strtoul(dbarg, NULL, 0);
317 for(i=0; i<dbmap->num; i++) {
319 if (id == dbmap->dbs[i].dbid) {
324 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
326 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
330 if (strcmp(name, dbarg) == 0) {
331 id = dbmap->dbs[i].dbid;
338 if (found && dbid_given && dbname != NULL) {
339 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
341 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
348 if (dbid) *dbid = id;
349 if (dbname) *dbname = talloc_strdup(ctdb, name);
350 if (flags) *flags = dbmap->dbs[i].flags;
352 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
356 talloc_free(tmp_ctx);
361 see if a process exists
363 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
371 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
372 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
376 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
378 printf("%u:%u exists\n", pnn, pid);
380 printf("%u:%u does not exist\n", pnn, pid);
386 display statistics structure
388 static void show_statistics(struct ctdb_statistics *s, int show_header)
390 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
392 const char *prefix=NULL;
394 int tmp, days, hours, minutes, seconds;
399 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
400 STATISTICS_FIELD(num_clients),
401 STATISTICS_FIELD(frozen),
402 STATISTICS_FIELD(recovering),
403 STATISTICS_FIELD(num_recoveries),
404 STATISTICS_FIELD(client_packets_sent),
405 STATISTICS_FIELD(client_packets_recv),
406 STATISTICS_FIELD(node_packets_sent),
407 STATISTICS_FIELD(node_packets_recv),
408 STATISTICS_FIELD(keepalive_packets_sent),
409 STATISTICS_FIELD(keepalive_packets_recv),
410 STATISTICS_FIELD(node.req_call),
411 STATISTICS_FIELD(node.reply_call),
412 STATISTICS_FIELD(node.req_dmaster),
413 STATISTICS_FIELD(node.reply_dmaster),
414 STATISTICS_FIELD(node.reply_error),
415 STATISTICS_FIELD(node.req_message),
416 STATISTICS_FIELD(node.req_control),
417 STATISTICS_FIELD(node.reply_control),
418 STATISTICS_FIELD(client.req_call),
419 STATISTICS_FIELD(client.req_message),
420 STATISTICS_FIELD(client.req_control),
421 STATISTICS_FIELD(timeouts.call),
422 STATISTICS_FIELD(timeouts.control),
423 STATISTICS_FIELD(timeouts.traverse),
424 STATISTICS_FIELD(locks.num_calls),
425 STATISTICS_FIELD(locks.num_current),
426 STATISTICS_FIELD(locks.num_pending),
427 STATISTICS_FIELD(locks.num_failed),
428 STATISTICS_FIELD(total_calls),
429 STATISTICS_FIELD(pending_calls),
430 STATISTICS_FIELD(childwrite_calls),
431 STATISTICS_FIELD(pending_childwrite_calls),
432 STATISTICS_FIELD(memory_used),
433 STATISTICS_FIELD(max_hop_count),
434 STATISTICS_FIELD(total_ro_delegations),
435 STATISTICS_FIELD(total_ro_revokes),
438 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
447 if (options.machinereadable){
449 printf("CTDB version:");
450 printf("Current time of statistics:");
451 printf("Statistics collected since:");
452 for (i=0;i<ARRAY_SIZE(fields);i++) {
453 printf("%s:", fields[i].name);
455 printf("num_reclock_ctdbd_latency:");
456 printf("min_reclock_ctdbd_latency:");
457 printf("avg_reclock_ctdbd_latency:");
458 printf("max_reclock_ctdbd_latency:");
460 printf("num_reclock_recd_latency:");
461 printf("min_reclock_recd_latency:");
462 printf("avg_reclock_recd_latency:");
463 printf("max_reclock_recd_latency:");
465 printf("num_call_latency:");
466 printf("min_call_latency:");
467 printf("avg_call_latency:");
468 printf("max_call_latency:");
470 printf("num_lockwait_latency:");
471 printf("min_lockwait_latency:");
472 printf("avg_lockwait_latency:");
473 printf("max_lockwait_latency:");
475 printf("num_childwrite_latency:");
476 printf("min_childwrite_latency:");
477 printf("avg_childwrite_latency:");
478 printf("max_childwrite_latency:");
481 printf("%d:", CTDB_VERSION);
482 printf("%d:", (int)s->statistics_current_time.tv_sec);
483 printf("%d:", (int)s->statistics_start_time.tv_sec);
484 for (i=0;i<ARRAY_SIZE(fields);i++) {
485 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
487 printf("%d:", s->reclock.ctdbd.num);
488 printf("%.6f:", s->reclock.ctdbd.min);
489 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
490 printf("%.6f:", s->reclock.ctdbd.max);
492 printf("%d:", s->reclock.recd.num);
493 printf("%.6f:", s->reclock.recd.min);
494 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
495 printf("%.6f:", s->reclock.recd.max);
497 printf("%d:", s->call_latency.num);
498 printf("%.6f:", s->call_latency.min);
499 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
500 printf("%.6f:", s->call_latency.max);
502 printf("%d:", s->childwrite_latency.num);
503 printf("%.6f:", s->childwrite_latency.min);
504 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
505 printf("%.6f:", s->childwrite_latency.max);
508 printf("CTDB version %u\n", CTDB_VERSION);
509 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
510 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
512 for (i=0;i<ARRAY_SIZE(fields);i++) {
513 if (strchr(fields[i].name, '.')) {
514 preflen = strcspn(fields[i].name, ".")+1;
515 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
516 prefix = fields[i].name;
517 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
522 printf(" %*s%-22s%*s%10u\n",
524 fields[i].name+preflen,
526 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
528 printf(" hop_count_buckets:");
529 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
530 printf(" %d", s->hop_count_bucket[i]);
533 printf(" lock_buckets:");
534 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
535 printf(" %d", s->locks.buckets[i]);
538 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);
540 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);
542 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);
544 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);
545 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);
548 talloc_free(tmp_ctx);
552 display remote ctdb statistics combined from all nodes
554 static int control_statistics_all(struct ctdb_context *ctdb)
557 struct ctdb_statistics statistics;
561 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
562 CTDB_NO_MEMORY(ctdb, nodes);
564 ZERO_STRUCT(statistics);
566 for (i=0;i<num_nodes;i++) {
567 struct ctdb_statistics s1;
569 uint32_t *v1 = (uint32_t *)&s1;
570 uint32_t *v2 = (uint32_t *)&statistics;
572 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
573 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
575 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
578 for (j=0;j<num_ints;j++) {
581 statistics.max_hop_count =
582 MAX(statistics.max_hop_count, s1.max_hop_count);
583 statistics.call_latency.max =
584 MAX(statistics.call_latency.max, s1.call_latency.max);
587 printf("Gathered statistics for %u nodes\n", num_nodes);
588 show_statistics(&statistics, 1);
593 display remote ctdb statistics
595 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
598 struct ctdb_statistics statistics;
600 if (options.pnn == CTDB_BROADCAST_ALL) {
601 return control_statistics_all(ctdb);
604 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
606 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
609 show_statistics(&statistics, 1);
615 reset remote ctdb statistics
617 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
621 ret = ctdb_statistics_reset(ctdb, options.pnn);
623 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
631 display remote ctdb rolling statistics
633 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
636 struct ctdb_statistics_wire *stats;
637 int i, num_records = -1;
639 assert_single_node_only();
642 num_records = atoi(argv[0]) - 1;
645 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
647 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
650 for (i=0;i<stats->num;i++) {
651 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
654 show_statistics(&stats->stats[i], i==0);
655 if (i == num_records) {
664 display remote ctdb db statistics
666 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
668 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
669 struct ctdb_db_statistics *dbstat;
679 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
683 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
685 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
686 talloc_free(tmp_ctx);
690 printf("DB Statistics: %s\n", argv[0]);
691 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
692 dbstat->db_ro_delegations);
693 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
694 dbstat->db_ro_delegations);
695 printf(" %s\n", "locks");
696 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
697 dbstat->locks.num_calls);
698 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
699 dbstat->locks.num_failed);
700 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
701 dbstat->locks.num_current);
702 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
703 dbstat->locks.num_pending);
704 printf(" %s", "hop_count_buckets:");
705 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
706 printf(" %d", dbstat->hop_count_bucket[i]);
709 printf(" %s", "lock_buckets:");
710 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
711 printf(" %d", dbstat->locks.buckets[i]);
714 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
715 "locks_latency MIN/AVG/MAX",
716 dbstat->locks.latency.min,
717 (dbstat->locks.latency.num ?
718 dbstat->locks.latency.total /dbstat->locks.latency.num :
720 dbstat->locks.latency.max,
721 dbstat->locks.latency.num);
723 for (i=0; i<dbstat->num_hot_keys; i++) {
724 if (dbstat->hot_keys[i].count > 0) {
728 dbstat->num_hot_keys = num_hot_keys;
730 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
731 for (i = 0; i < dbstat->num_hot_keys; i++) {
733 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
734 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
735 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
740 talloc_free(tmp_ctx);
745 display uptime of remote node
747 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
750 struct ctdb_uptime *uptime = NULL;
751 int tmp, days, hours, minutes, seconds;
753 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
755 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
759 if (options.machinereadable){
760 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
761 printf(":%u:%u:%u:%lf\n",
762 (unsigned int)uptime->current_time.tv_sec,
763 (unsigned int)uptime->ctdbd_start_time.tv_sec,
764 (unsigned int)uptime->last_recovery_finished.tv_sec,
765 timeval_delta(&uptime->last_recovery_finished,
766 &uptime->last_recovery_started)
771 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
773 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
781 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
783 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
791 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
793 printf("Duration of last recovery/failover: %lf seconds\n",
794 timeval_delta(&uptime->last_recovery_finished,
795 &uptime->last_recovery_started));
801 show the PNN of the current node
803 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
807 mypnn = getpnn(ctdb);
809 printf("PNN:%d\n", mypnn);
815 struct pnn_node *next, *prev;
820 static struct pnn_node *read_pnn_node_file(TALLOC_CTX *mem_ctx,
826 struct pnn_node *pnn_nodes = NULL;
827 struct pnn_node *pnn_node;
829 lines = file_lines_load(file, &nlines, mem_ctx);
833 for (i=0, pnn=0; i<nlines; i++) {
837 /* strip leading spaces */
838 while((*node == ' ') || (*node == '\t')) {
845 if (strcmp(node, "") == 0) {
848 pnn_node = talloc(mem_ctx, struct pnn_node);
849 pnn_node->pnn = pnn++;
851 if (!parse_ip(node, NULL, 0, &pnn_node->addr)) {
853 ("Invalid IP address '%s' in file %s\n",
855 /* Caller will free mem_ctx */
859 DLIST_ADD_END(pnn_nodes, pnn_node, NULL);
865 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
867 const char *nodes_list;
869 /* read the nodes file */
870 nodes_list = getenv("CTDB_NODES");
871 if (nodes_list == NULL) {
872 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
873 getenv("CTDB_BASE"));
874 if (nodes_list == NULL) {
875 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
880 return read_pnn_node_file(mem_ctx, nodes_list);
884 show the PNN of the current node
885 discover the pnn by loading the nodes file and try to bind to all
886 addresses one at a time until the ip address is found.
888 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
890 TALLOC_CTX *mem_ctx = talloc_new(NULL);
891 struct pnn_node *pnn_nodes;
892 struct pnn_node *pnn_node;
894 assert_single_node_only();
896 pnn_nodes = read_nodes_file(mem_ctx);
897 if (pnn_nodes == NULL) {
898 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
899 talloc_free(mem_ctx);
903 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
904 if (ctdb_sys_have_ip(&pnn_node->addr)) {
905 printf("PNN:%d\n", pnn_node->pnn);
906 talloc_free(mem_ctx);
911 printf("Failed to detect which PNN this node is\n");
912 talloc_free(mem_ctx);
916 /* Helpers for ctdb status
918 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
920 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
924 if (node->flags == 0) {
925 struct ctdb_control_get_ifaces *ifaces;
927 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
928 tmp_ctx, &ifaces) == 0) {
929 for (j=0; j < ifaces->num; j++) {
930 if (ifaces->ifaces[j].link_state != 0) {
938 talloc_free(tmp_ctx);
943 static void control_status_header_machine(void)
945 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
946 ":Inactive:PartiallyOnline:ThisNode:\n");
949 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
950 struct ctdb_node_and_flags *node)
952 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
953 ctdb_addr_to_str(&node->addr),
954 !!(node->flags&NODE_FLAGS_DISCONNECTED),
955 !!(node->flags&NODE_FLAGS_BANNED),
956 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
957 !!(node->flags&NODE_FLAGS_UNHEALTHY),
958 !!(node->flags&NODE_FLAGS_STOPPED),
959 !!(node->flags&NODE_FLAGS_INACTIVE),
960 is_partially_online(ctdb, node) ? 1 : 0,
961 (node->pnn == mypnn)?'Y':'N');
966 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
967 struct ctdb_node_and_flags *node)
969 printf("pnn:%d %-16s %s%s\n", node->pnn,
970 ctdb_addr_to_str(&node->addr),
971 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
972 node->pnn == mypnn?" (THIS NODE)":"");
978 display remote ctdb status
980 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
982 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
984 struct ctdb_vnn_map *vnnmap=NULL;
985 struct ctdb_node_map *nodemap=NULL;
986 uint32_t recmode, recmaster, mypnn;
987 int num_deleted_nodes = 0;
990 mypnn = getpnn(ctdb);
992 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
994 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
995 talloc_free(tmp_ctx);
999 if (options.machinereadable) {
1000 control_status_header_machine();
1001 for (i=0;i<nodemap->num;i++) {
1002 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1005 (void) control_status_1_machine(ctdb, mypnn,
1006 &nodemap->nodes[i]);
1008 talloc_free(tmp_ctx);
1012 for (i=0; i<nodemap->num; i++) {
1013 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1014 num_deleted_nodes++;
1017 if (num_deleted_nodes == 0) {
1018 printf("Number of nodes:%d\n", nodemap->num);
1020 printf("Number of nodes:%d (including %d deleted nodes)\n",
1021 nodemap->num, num_deleted_nodes);
1023 for(i=0;i<nodemap->num;i++){
1024 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1027 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1030 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1032 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1033 talloc_free(tmp_ctx);
1036 if (vnnmap->generation == INVALID_GENERATION) {
1037 printf("Generation:INVALID\n");
1039 printf("Generation:%d\n",vnnmap->generation);
1041 printf("Size:%d\n",vnnmap->size);
1042 for(i=0;i<vnnmap->size;i++){
1043 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1046 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1048 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1049 talloc_free(tmp_ctx);
1052 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1054 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1056 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1057 talloc_free(tmp_ctx);
1060 printf("Recovery master:%d\n",recmaster);
1062 talloc_free(tmp_ctx);
1066 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1068 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1070 struct ctdb_node_map *nodemap=NULL;
1072 uint32_t pnn_mode, mypnn;
1078 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1079 options.pnn, true, &nodes, &pnn_mode)) {
1083 if (options.machinereadable) {
1084 control_status_header_machine();
1085 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1086 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1089 mypnn = getpnn(ctdb);
1091 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1093 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1094 talloc_free(tmp_ctx);
1100 for (i = 0; i < talloc_array_length(nodes); i++) {
1101 if (options.machinereadable) {
1102 ret |= control_status_1_machine(ctdb, mypnn,
1103 &nodemap->nodes[nodes[i]]);
1105 ret |= control_status_1_human(ctdb, mypnn,
1106 &nodemap->nodes[nodes[i]]);
1110 talloc_free(tmp_ctx);
1114 static struct pnn_node *read_natgw_nodes_file(struct ctdb_context *ctdb,
1115 TALLOC_CTX *mem_ctx)
1117 const char *natgw_list;
1118 struct pnn_node *natgw_nodes = NULL;
1120 natgw_list = getenv("CTDB_NATGW_NODES");
1121 if (natgw_list == NULL) {
1122 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1123 getenv("CTDB_BASE"));
1124 if (natgw_list == NULL) {
1125 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1129 /* The PNNs will be junk but they're not used */
1130 natgw_nodes = read_pnn_node_file(mem_ctx, natgw_list);
1131 if (natgw_nodes == NULL) {
1133 ("Failed to load natgw node list '%s'\n", natgw_list));
1139 /* talloc off the existing nodemap... */
1140 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1142 return talloc_zero_size(nodemap,
1143 offsetof(struct ctdb_node_map, nodes) +
1144 nodemap->num * sizeof(struct ctdb_node_and_flags));
1147 static struct ctdb_node_map *
1148 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1149 struct ctdb_node_map *nodemap,
1150 struct pnn_node *nodes)
1154 struct ctdb_node_map *ret;
1156 ret = talloc_nodemap(nodemap);
1157 CTDB_NO_MEMORY_NULL(ctdb, ret);
1161 for (i = 0; i < nodemap->num; i++) {
1162 for(n = nodes; n != NULL ; n = n->next) {
1163 if (ctdb_same_ip(&n->addr,
1164 &nodemap->nodes[i].addr)) {
1172 ret->nodes[ret->num] = nodemap->nodes[i];
1179 static struct ctdb_node_map *
1180 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1181 struct ctdb_node_map *nodemap,
1182 uint32_t required_capabilities)
1185 uint32_t capabilities;
1186 struct ctdb_node_map *ret;
1188 ret = talloc_nodemap(nodemap);
1189 CTDB_NO_MEMORY_NULL(ctdb, ret);
1193 for (i = 0; i < nodemap->num; i++) {
1196 /* Disconnected nodes have no capabilities! */
1197 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1201 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1202 nodemap->nodes[i].pnn,
1205 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1206 nodemap->nodes[i].pnn));
1210 if (!(capabilities & required_capabilities)) {
1214 ret->nodes[ret->num] = nodemap->nodes[i];
1221 static struct ctdb_node_map *
1222 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1223 struct ctdb_node_map *nodemap,
1224 uint32_t flags_mask)
1227 struct ctdb_node_map *ret;
1229 ret = talloc_nodemap(nodemap);
1230 CTDB_NO_MEMORY_NULL(ctdb, ret);
1234 for (i = 0; i < nodemap->num; i++) {
1235 if (nodemap->nodes[i].flags & flags_mask) {
1239 ret->nodes[ret->num] = nodemap->nodes[i];
1247 display the list of nodes belonging to this natgw configuration
1249 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1251 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1253 struct pnn_node *natgw_nodes = NULL;
1254 struct ctdb_node_map *orig_nodemap=NULL;
1255 struct ctdb_node_map *cnodemap, *nodemap;
1256 uint32_t mypnn, pnn;
1259 /* When we have some nodes that could be the NATGW, make a
1260 * series of attempts to find the first node that doesn't have
1261 * certain status flags set.
1263 uint32_t exclude_flags[] = {
1264 /* Look for a nice healthy node */
1265 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1266 /* If not found, an UNHEALTHY/BANNED node will do */
1267 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1268 /* If not found, a STOPPED node will do */
1269 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1273 /* read the natgw nodes file into a linked list */
1274 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1275 if (natgw_nodes == NULL) {
1280 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1281 tmp_ctx, &orig_nodemap);
1283 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1284 talloc_free(tmp_ctx);
1288 /* Get a nodemap that includes only the nodes in the NATGW
1290 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1291 if (nodemap == NULL) {
1296 /* Get a nodemap that includes only the nodes with the NATGW
1298 cnodemap = filter_nodemap_by_capabilities(ctdb, nodemap,
1300 if (cnodemap == NULL) {
1305 ret = 2; /* matches ENOENT */
1308 for (i = 0; exclude_flags[i] != 0; i++) {
1309 struct ctdb_node_map *t =
1310 filter_nodemap_by_flags(ctdb, cnodemap,
1319 pnn = t->nodes[0].pnn;
1320 ip = ctdb_addr_to_str(&t->nodes[0].addr);
1326 if (options.machinereadable) {
1327 printf(":Node:IP:\n");
1328 printf(":%d:%s:\n", pnn, ip);
1330 printf("%d %s\n", pnn, ip);
1333 /* print the pruned list of nodes belonging to this natgw list */
1334 mypnn = getpnn(ctdb);
1335 if (options.machinereadable) {
1336 control_status_header_machine();
1338 printf("Number of nodes:%d\n", nodemap->num);
1340 for(i=0;i<nodemap->num;i++){
1341 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1344 if (options.machinereadable) {
1345 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1347 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1352 talloc_free(tmp_ctx);
1357 display the status of the scripts for monitoring (or other events)
1359 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1360 enum ctdb_eventscript_call type)
1362 struct ctdb_scripts_wire *script_status;
1365 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1367 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1371 if (script_status == NULL) {
1372 if (!options.machinereadable) {
1373 printf("%s cycle never run\n",
1374 ctdb_eventscript_call_names[type]);
1379 if (!options.machinereadable) {
1380 printf("%d scripts were executed last %s cycle\n",
1381 script_status->num_scripts,
1382 ctdb_eventscript_call_names[type]);
1384 for (i=0; i<script_status->num_scripts; i++) {
1385 const char *status = NULL;
1387 switch (script_status->scripts[i].status) {
1389 status = "TIMEDOUT";
1392 status = "DISABLED";
1398 if (script_status->scripts[i].status > 0)
1402 if (options.machinereadable) {
1403 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1404 ctdb_eventscript_call_names[type],
1405 script_status->scripts[i].name,
1406 script_status->scripts[i].status,
1408 (long)script_status->scripts[i].start.tv_sec,
1409 (long)script_status->scripts[i].start.tv_usec,
1410 (long)script_status->scripts[i].finished.tv_sec,
1411 (long)script_status->scripts[i].finished.tv_usec,
1412 script_status->scripts[i].output);
1416 printf("%-20s Status:%s ",
1417 script_status->scripts[i].name, status);
1419 /* Some other error, eg from stat. */
1420 printf("%-20s Status:CANNOT RUN (%s)",
1421 script_status->scripts[i].name,
1422 strerror(-script_status->scripts[i].status));
1424 if (script_status->scripts[i].status >= 0) {
1425 printf("Duration:%.3lf ",
1426 timeval_delta(&script_status->scripts[i].finished,
1427 &script_status->scripts[i].start));
1429 if (script_status->scripts[i].status != -ENOEXEC) {
1431 ctime(&script_status->scripts[i].start.tv_sec));
1432 if (script_status->scripts[i].status != 0) {
1433 printf(" OUTPUT:%s\n",
1434 script_status->scripts[i].output);
1444 static int control_scriptstatus(struct ctdb_context *ctdb,
1445 int argc, const char **argv)
1448 enum ctdb_eventscript_call type, min, max;
1452 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1457 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1461 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1462 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1468 if (type == CTDB_EVENT_MAX) {
1469 if (strcmp(arg, "all") == 0) {
1471 max = CTDB_EVENT_MAX;
1473 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1478 if (options.machinereadable) {
1479 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1482 for (type = min; type < max; type++) {
1483 ret = control_one_scriptstatus(ctdb, type);
1493 enable an eventscript
1495 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1503 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1505 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1513 disable an eventscript
1515 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1523 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1525 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1533 display the pnn of the recovery master
1535 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1540 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1542 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1545 printf("%d\n",recmaster);
1551 add a tickle to a public address
1553 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1555 struct ctdb_tcp_connection t;
1559 assert_single_node_only();
1565 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1566 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1569 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1570 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1574 data.dptr = (uint8_t *)&t;
1575 data.dsize = sizeof(t);
1577 /* tell all nodes about this tcp connection */
1578 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1579 0, data, ctdb, NULL, NULL, NULL, NULL);
1581 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1590 delete a tickle from a node
1592 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1594 struct ctdb_tcp_connection t;
1598 assert_single_node_only();
1604 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1605 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1608 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1609 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1613 data.dptr = (uint8_t *)&t;
1614 data.dsize = sizeof(t);
1616 /* tell all nodes about this tcp connection */
1617 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1618 0, data, ctdb, NULL, NULL, NULL, NULL);
1620 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1629 get a list of all tickles for this pnn
1631 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1633 struct ctdb_control_tcp_tickle_list *list;
1634 ctdb_sock_addr addr;
1638 assert_single_node_only();
1645 port = atoi(argv[1]);
1648 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1649 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1653 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1655 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1659 if (options.machinereadable){
1660 printf(":source ip:port:destination ip:port:\n");
1661 for (i=0;i<list->tickles.num;i++) {
1662 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1665 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1666 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1669 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1670 printf("Num tickles:%u\n", list->tickles.num);
1671 for (i=0;i<list->tickles.num;i++) {
1672 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1675 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1676 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));
1686 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1688 struct ctdb_all_public_ips *ips;
1689 struct ctdb_public_ip ip;
1692 uint32_t disable_time;
1694 struct ctdb_node_map *nodemap=NULL;
1695 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1698 data.dptr = (uint8_t*)&disable_time;
1699 data.dsize = sizeof(disable_time);
1700 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1702 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1708 /* read the public ip list from the node */
1709 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1711 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1712 talloc_free(tmp_ctx);
1716 for (i=0;i<ips->num;i++) {
1717 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1722 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1723 pnn, ctdb_addr_to_str(addr)));
1724 talloc_free(tmp_ctx);
1731 data.dptr = (uint8_t *)&ip;
1732 data.dsize = sizeof(ip);
1734 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1736 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1737 talloc_free(tmp_ctx);
1741 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1742 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1749 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1750 talloc_free(tmp_ctx);
1754 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1756 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1757 talloc_free(tmp_ctx);
1761 /* update the recovery daemon so it now knows to expect the new
1762 node assignment for this ip.
1764 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1766 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1770 talloc_free(tmp_ctx);
1776 * scans all other nodes and returns a pnn for another node that can host this
1780 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1782 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1783 struct ctdb_all_public_ips *ips;
1784 struct ctdb_node_map *nodemap=NULL;
1787 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1789 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1790 talloc_free(tmp_ctx);
1794 for(i=0;i<nodemap->num;i++){
1795 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1798 if (nodemap->nodes[i].pnn == options.pnn) {
1802 /* read the public ip list from this node */
1803 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1805 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1809 for (j=0;j<ips->num;j++) {
1810 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1811 talloc_free(tmp_ctx);
1812 return nodemap->nodes[i].pnn;
1818 talloc_free(tmp_ctx);
1822 /* If pnn is -1 then try to find a node to move IP to... */
1823 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1825 bool pnn_specified = (pnn == -1 ? false : true);
1828 while (retries < 5) {
1829 if (!pnn_specified) {
1830 pnn = find_other_host_for_public_ip(ctdb, addr);
1835 ("Trying to move public IP to node %u\n", pnn));
1838 if (move_ip(ctdb, addr, pnn) == 0) {
1851 move/failover an ip address to a specific node
1853 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1856 ctdb_sock_addr addr;
1858 assert_single_node_only();
1865 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1866 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1871 if (sscanf(argv[1], "%u", &pnn) != 1) {
1872 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1876 if (!try_moveip(ctdb, &addr, pnn)) {
1877 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1884 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1888 data.dptr = (uint8_t *)&pnn;
1889 data.dsize = sizeof(uint32_t);
1890 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1892 ("Failed to send message to force node %u to be a rebalancing target\n",
1902 rebalance a node by setting it to allow failback and triggering a
1905 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1907 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1912 assert_single_node_only();
1918 /* Determine the nodes where IPs need to be reloaded */
1919 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1920 options.pnn, true, &nodes, &pnn_mode)) {
1925 for (i = 0; i < talloc_array_length(nodes); i++) {
1926 if (!rebalance_node(ctdb, nodes[i])) {
1932 talloc_free(tmp_ctx);
1936 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1938 struct ctdb_public_ip ip;
1941 uint32_t disable_time;
1943 struct ctdb_node_map *nodemap=NULL;
1944 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1947 data.dptr = (uint8_t*)&disable_time;
1948 data.dsize = sizeof(disable_time);
1949 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1951 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1958 data.dptr = (uint8_t *)&ip;
1959 data.dsize = sizeof(ip);
1961 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1963 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1964 talloc_free(tmp_ctx);
1968 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1969 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1976 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1977 talloc_free(tmp_ctx);
1981 talloc_free(tmp_ctx);
1986 release an ip form all nodes and have it re-assigned by recd
1988 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1990 ctdb_sock_addr addr;
1992 assert_single_node_only();
1999 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2000 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2004 if (rebalance_ip(ctdb, &addr) != 0) {
2005 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2012 static int getips_store_callback(void *param, void *data)
2014 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2015 struct ctdb_all_public_ips *ips = param;
2019 ips->ips[i].pnn = node_ip->pnn;
2020 ips->ips[i].addr = node_ip->addr;
2024 static int getips_count_callback(void *param, void *data)
2026 uint32_t *count = param;
2033 static uint32_t *ip_key(ctdb_sock_addr *ip)
2035 static uint32_t key[IP_KEYLEN];
2037 bzero(key, sizeof(key));
2039 switch (ip->sa.sa_family) {
2041 key[0] = ip->ip.sin_addr.s_addr;
2044 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2052 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2059 static void *add_ip_callback(void *parm, void *data)
2065 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2067 struct ctdb_all_public_ips *tmp_ips;
2068 struct ctdb_node_map *nodemap=NULL;
2069 trbt_tree_t *ip_tree;
2073 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2075 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2079 ip_tree = trbt_create(tmp_ctx, 0);
2081 for(i=0;i<nodemap->num;i++){
2082 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2085 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2089 /* read the public ip list from this node */
2090 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2092 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2096 for (j=0; j<tmp_ips->num;j++) {
2097 struct ctdb_public_ip *node_ip;
2099 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2100 node_ip->pnn = tmp_ips->ips[j].pnn;
2101 node_ip->addr = tmp_ips->ips[j].addr;
2103 trbt_insertarray32_callback(ip_tree,
2104 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2108 talloc_free(tmp_ips);
2113 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2115 len = offsetof(struct ctdb_all_public_ips, ips) +
2116 count*sizeof(struct ctdb_public_ip);
2117 tmp_ips = talloc_zero_size(tmp_ctx, len);
2118 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2126 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2128 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2130 event_add_timed(ctdb->ev, ctdb,
2131 timeval_current_ofs(1, 0),
2132 ctdb_every_second, ctdb);
2135 struct srvid_reply_handler_data {
2139 const char *srvid_str;
2142 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2147 struct srvid_reply_handler_data *d =
2148 (struct srvid_reply_handler_data *)private_data;
2152 if (data.dsize != sizeof(ret)) {
2153 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2157 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2158 ret = *(int32_t *)data.dptr;
2161 ("%s failed with result %d\n", d->srvid_str, ret));
2165 if (!d->wait_for_all) {
2170 /* Wait for all replies */
2172 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2173 if (d->nodes[i] == ret) {
2175 ("%s reply received from node %u\n",
2176 d->srvid_str, ret));
2179 if (d->nodes[i] != -1) {
2180 /* Found a node that hasn't yet replied */
2186 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2187 * or replies from all connected nodes. arg is the data argument to
2188 * pass in the srvid_request structure - pass 0 if this isn't needed.
2190 static int srvid_broadcast(struct ctdb_context *ctdb,
2191 uint64_t srvid, uint32_t *arg,
2192 const char *srvid_str, bool wait_for_all)
2197 uint64_t reply_srvid;
2198 struct srvid_request request;
2199 struct srvid_request_data request_data;
2200 struct srvid_reply_handler_data reply_data;
2203 ZERO_STRUCT(request);
2205 /* Time ticks to enable timeouts to be processed */
2206 event_add_timed(ctdb->ev, ctdb,
2207 timeval_current_ofs(1, 0),
2208 ctdb_every_second, ctdb);
2210 pnn = ctdb_get_pnn(ctdb);
2211 reply_srvid = getpid();
2215 request.srvid = reply_srvid;
2217 data.dptr = (uint8_t *)&request;
2218 data.dsize = sizeof(request);
2220 request_data.pnn = pnn;
2221 request_data.srvid = reply_srvid;
2222 request_data.data = *arg;
2224 data.dptr = (uint8_t *)&request_data;
2225 data.dsize = sizeof(request_data);
2228 /* Register message port for reply from recovery master */
2229 ctdb_client_set_message_handler(ctdb, reply_srvid,
2230 srvid_broadcast_reply_handler,
2233 reply_data.wait_for_all = wait_for_all;
2234 reply_data.nodes = NULL;
2235 reply_data.srvid_str = srvid_str;
2238 reply_data.done = false;
2241 struct ctdb_node_map *nodemap;
2243 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2244 CTDB_CURRENT_NODE, ctdb, &nodemap);
2247 ("Unable to get nodemap from current node, try again\n"));
2252 if (reply_data.nodes != NULL) {
2253 talloc_free(reply_data.nodes);
2255 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2258 talloc_free(nodemap);
2261 /* Send to all connected nodes. Only recmaster replies */
2262 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2265 /* This can only happen if the socket is closed and
2266 * there's no way to recover from that, so don't try
2270 ("Failed to send %s request to connected nodes\n",
2275 tv = timeval_current();
2276 /* This loop terminates the reply is received */
2277 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2278 event_loop_once(ctdb->ev);
2281 if (!reply_data.done) {
2283 ("Still waiting for confirmation of %s\n", srvid_str));
2288 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2290 talloc_free(reply_data.nodes);
2295 static int ipreallocate(struct ctdb_context *ctdb)
2297 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2298 "IP reallocation", false);
2302 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2304 return ipreallocate(ctdb);
2308 add a public ip address to a node
2310 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2313 int len, retries = 0;
2315 ctdb_sock_addr addr;
2316 struct ctdb_control_ip_iface *pub;
2317 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2318 struct ctdb_all_public_ips *ips;
2322 talloc_free(tmp_ctx);
2326 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2327 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2328 talloc_free(tmp_ctx);
2332 /* read the public ip list from the node */
2333 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2335 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2336 talloc_free(tmp_ctx);
2339 for (i=0;i<ips->num;i++) {
2340 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2341 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2348 /* Dont timeout. This command waits for an ip reallocation
2349 which sometimes can take wuite a while if there has
2350 been a recent recovery
2354 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2355 pub = talloc_size(tmp_ctx, len);
2356 CTDB_NO_MEMORY(ctdb, pub);
2360 pub->len = strlen(argv[1])+1;
2361 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2364 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2366 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2370 } while (retries < 5 && ret != 0);
2372 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2373 talloc_free(tmp_ctx);
2377 if (rebalance_node(ctdb, options.pnn) != 0) {
2378 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2382 talloc_free(tmp_ctx);
2387 add a public ip address to a node
2389 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2391 ctdb_sock_addr addr;
2397 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2398 printf("Badly formed ip : %s\n", argv[0]);
2402 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2407 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2409 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2411 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2412 struct ctdb_node_map *nodemap=NULL;
2413 struct ctdb_all_public_ips *ips;
2416 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2418 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2422 /* remove it from the nodes that are not hosting the ip currently */
2423 for(i=0;i<nodemap->num;i++){
2424 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2427 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2429 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2433 for (j=0;j<ips->num;j++) {
2434 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2442 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2446 options.pnn = nodemap->nodes[i].pnn;
2447 control_delip(ctdb, argc, argv);
2451 /* remove it from every node (also the one hosting it) */
2452 for(i=0;i<nodemap->num;i++){
2453 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2456 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2458 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2462 for (j=0;j<ips->num;j++) {
2463 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2471 options.pnn = nodemap->nodes[i].pnn;
2472 control_delip(ctdb, argc, argv);
2475 talloc_free(tmp_ctx);
2480 delete a public ip address from a node
2482 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2485 ctdb_sock_addr addr;
2486 struct ctdb_control_ip_iface pub;
2487 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2488 struct ctdb_all_public_ips *ips;
2491 talloc_free(tmp_ctx);
2495 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2496 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2500 if (options.pnn == CTDB_BROADCAST_ALL) {
2501 return control_delip_all(ctdb, argc, argv, &addr);
2508 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2510 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2511 talloc_free(tmp_ctx);
2515 for (i=0;i<ips->num;i++) {
2516 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2522 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2523 ctdb_addr_to_str(&addr)));
2524 talloc_free(tmp_ctx);
2528 /* This is an optimisation. If this node is hosting the IP
2529 * then try to move it somewhere else without invoking a full
2530 * takeover run. We don't care if this doesn't work!
2532 if (ips->ips[i].pnn == options.pnn) {
2533 (void) try_moveip(ctdb, &addr, -1);
2536 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2538 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2539 talloc_free(tmp_ctx);
2543 talloc_free(tmp_ctx);
2547 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2548 int argc, const char **argv)
2550 struct ctdb_control_killtcp *killtcp;
2551 int max_entries, current, i;
2552 struct timeval timeout;
2553 char line[128], src[128], dst[128];
2556 struct client_async_data *async_data;
2557 struct ctdb_client_control_state *state;
2567 while (!feof(stdin)) {
2568 if (fgets(line, sizeof(line), stdin) == NULL) {
2572 /* Silently skip empty lines */
2573 if (line[0] == '\n') {
2577 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2578 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2580 talloc_free(killtcp);
2584 if (current >= max_entries) {
2585 max_entries += 1024;
2586 killtcp = talloc_realloc(ctdb, killtcp,
2587 struct ctdb_control_killtcp,
2589 CTDB_NO_MEMORY(ctdb, killtcp);
2592 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2593 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2595 talloc_free(killtcp);
2599 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2600 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2602 talloc_free(killtcp);
2609 async_data = talloc_zero(ctdb, struct client_async_data);
2610 if (async_data == NULL) {
2611 talloc_free(killtcp);
2615 for (i = 0; i < current; i++) {
2617 data.dsize = sizeof(struct ctdb_control_killtcp);
2618 data.dptr = (unsigned char *)&killtcp[i];
2620 timeout = TIMELIMIT();
2621 state = ctdb_control_send(ctdb, options.pnn, 0,
2622 CTDB_CONTROL_KILL_TCP, 0, data,
2623 async_data, &timeout, NULL);
2625 if (state == NULL) {
2627 ("Failed to call async killtcp control to node %u\n",
2629 talloc_free(killtcp);
2633 ctdb_client_async_add(async_data, state);
2636 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2637 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2638 talloc_free(killtcp);
2642 talloc_free(killtcp);
2648 kill a tcp connection
2650 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2653 struct ctdb_control_killtcp killtcp;
2655 assert_single_node_only();
2658 return kill_tcp_from_file(ctdb, argc, argv);
2665 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2666 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2670 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2671 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2675 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2677 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2688 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2691 ctdb_sock_addr addr;
2693 assert_single_node_only();
2699 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2700 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2704 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2706 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2714 register a server id
2716 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2719 struct ctdb_server_id server_id;
2725 server_id.pnn = strtoul(argv[0], NULL, 0);
2726 server_id.type = strtoul(argv[1], NULL, 0);
2727 server_id.server_id = strtoul(argv[2], NULL, 0);
2729 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2731 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2734 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2740 unregister a server id
2742 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2745 struct ctdb_server_id server_id;
2751 server_id.pnn = strtoul(argv[0], NULL, 0);
2752 server_id.type = strtoul(argv[1], NULL, 0);
2753 server_id.server_id = strtoul(argv[2], NULL, 0);
2755 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2757 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2764 check if a server id exists
2766 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2770 struct ctdb_server_id server_id;
2776 server_id.pnn = strtoul(argv[0], NULL, 0);
2777 server_id.type = strtoul(argv[1], NULL, 0);
2778 server_id.server_id = strtoul(argv[2], NULL, 0);
2780 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2782 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2787 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2789 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2795 get a list of all server ids that are registered on a node
2797 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2800 struct ctdb_server_id_list *server_ids;
2802 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2804 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2808 for (i=0; i<server_ids->num; i++) {
2809 printf("Server id %d:%d:%d\n",
2810 server_ids->server_ids[i].pnn,
2811 server_ids->server_ids[i].type,
2812 server_ids->server_ids[i].server_id);
2819 check if a server id exists
2821 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2823 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2829 talloc_free(tmp_ctx);
2833 ids = talloc_array(tmp_ctx, uint64_t, argc);
2834 result = talloc_array(tmp_ctx, uint8_t, argc);
2836 for (i = 0; i < argc; i++) {
2837 ids[i] = strtoull(argv[i], NULL, 0);
2840 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2841 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2843 talloc_free(tmp_ctx);
2847 for (i=0; i < argc; i++) {
2848 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2849 result[i] ? "exists" : "does not exist");
2852 talloc_free(tmp_ctx);
2857 send a tcp tickle ack
2859 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2862 ctdb_sock_addr src, dst;
2868 if (!parse_ip_port(argv[0], &src)) {
2869 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2873 if (!parse_ip_port(argv[1], &dst)) {
2874 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2878 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2882 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2889 display public ip status
2891 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2894 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2895 struct ctdb_all_public_ips *ips;
2897 if (options.pnn == CTDB_BROADCAST_ALL) {
2898 /* read the list of public ips from all nodes */
2899 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2901 /* read the public ip list from this node */
2902 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2905 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2906 talloc_free(tmp_ctx);
2910 if (options.machinereadable){
2911 printf(":Public IP:Node:");
2912 if (options.verbose){
2913 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2917 if (options.pnn == CTDB_BROADCAST_ALL) {
2918 printf("Public IPs on ALL nodes\n");
2920 printf("Public IPs on node %u\n", options.pnn);
2924 for (i=1;i<=ips->num;i++) {
2925 struct ctdb_control_public_ip_info *info = NULL;
2927 char *aciface = NULL;
2928 char *avifaces = NULL;
2929 char *cifaces = NULL;
2931 if (options.pnn == CTDB_BROADCAST_ALL) {
2932 pnn = ips->ips[ips->num-i].pnn;
2938 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2939 &ips->ips[ips->num-i].addr, &info);
2946 for (j=0; j < info->num; j++) {
2947 if (cifaces == NULL) {
2948 cifaces = talloc_strdup(info,
2949 info->ifaces[j].name);
2951 cifaces = talloc_asprintf_append(cifaces,
2953 info->ifaces[j].name);
2956 if (info->active_idx == j) {
2957 aciface = info->ifaces[j].name;
2960 if (info->ifaces[j].link_state == 0) {
2964 if (avifaces == NULL) {
2965 avifaces = talloc_strdup(info, info->ifaces[j].name);
2967 avifaces = talloc_asprintf_append(avifaces,
2969 info->ifaces[j].name);
2974 if (options.machinereadable){
2976 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2977 ips->ips[ips->num-i].pnn);
2978 if (options.verbose){
2981 avifaces?avifaces:"",
2982 cifaces?cifaces:"");
2986 if (options.verbose) {
2987 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2988 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2989 ips->ips[ips->num-i].pnn,
2991 avifaces?avifaces:"",
2992 cifaces?cifaces:"");
2995 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2996 ips->ips[ips->num-i].pnn);
3002 talloc_free(tmp_ctx);
3009 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3012 ctdb_sock_addr addr;
3013 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3014 struct ctdb_control_public_ip_info *info;
3017 talloc_free(tmp_ctx);
3021 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3022 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3026 /* read the public ip info from this node */
3027 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3028 tmp_ctx, &addr, &info);
3030 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3031 argv[0], options.pnn));
3032 talloc_free(tmp_ctx);
3036 printf("Public IP[%s] info on node %u\n",
3037 ctdb_addr_to_str(&info->ip.addr),
3040 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3041 ctdb_addr_to_str(&info->ip.addr),
3042 info->ip.pnn, info->num);
3044 for (i=0; i<info->num; i++) {
3045 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3047 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3048 i+1, info->ifaces[i].name,
3049 info->ifaces[i].link_state?"up":"down",
3050 (unsigned int)info->ifaces[i].references,
3051 (i==info->active_idx)?" (active)":"");
3054 talloc_free(tmp_ctx);
3059 display interfaces status
3061 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3063 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3065 struct ctdb_control_get_ifaces *ifaces;
3068 /* read the public ip list from this node */
3069 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3071 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3073 talloc_free(tmp_ctx);
3077 if (options.machinereadable){
3078 printf(":Name:LinkStatus:References:\n");
3080 printf("Interfaces on node %u\n", options.pnn);
3083 for (i=0; i<ifaces->num; i++) {
3084 if (options.machinereadable){
3085 printf(":%s:%s:%u\n",
3086 ifaces->ifaces[i].name,
3087 ifaces->ifaces[i].link_state?"1":"0",
3088 (unsigned int)ifaces->ifaces[i].references);
3090 printf("name:%s link:%s references:%u\n",
3091 ifaces->ifaces[i].name,
3092 ifaces->ifaces[i].link_state?"up":"down",
3093 (unsigned int)ifaces->ifaces[i].references);
3097 talloc_free(tmp_ctx);
3103 set link status of an interface
3105 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3108 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3109 struct ctdb_control_iface_info info;
3117 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3118 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3120 talloc_free(tmp_ctx);
3123 strcpy(info.name, argv[0]);
3125 if (strcmp(argv[1], "up") == 0) {
3126 info.link_state = 1;
3127 } else if (strcmp(argv[1], "down") == 0) {
3128 info.link_state = 0;
3130 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3132 talloc_free(tmp_ctx);
3136 /* read the public ip list from this node */
3137 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3140 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3141 argv[0], options.pnn));
3142 talloc_free(tmp_ctx);
3146 talloc_free(tmp_ctx);
3151 display pid of a ctdb daemon
3153 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3158 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3160 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3163 printf("Pid:%d\n", pid);
3168 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3170 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3172 update_flags_handler_t handler,
3177 struct ctdb_node_map *nodemap = NULL;
3181 /* Check if the node is already in the desired state */
3182 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3184 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3187 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3188 if (set_flag == flag_is_set) {
3189 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3190 (set_flag ? "already" : "not"), desc));
3195 if (!handler(ctdb, data)) {
3196 DEBUG(DEBUG_WARNING,
3197 ("Failed to send control to set state %s on node %u, try again\n",
3198 desc, options.pnn));
3203 /* Read the nodemap and verify the change took effect.
3204 * Even if the above control/hanlder timed out then it
3205 * could still have worked!
3207 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3210 DEBUG(DEBUG_WARNING,
3211 ("Unable to get nodemap from local node, try again\n"));
3213 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3214 } while (nodemap == NULL || (set_flag != flag_is_set));
3216 return ipreallocate(ctdb);
3219 /* Administratively disable a node */
3220 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3224 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3225 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3229 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3231 return update_flags_and_ipreallocate(ctdb, NULL,
3232 update_flags_disabled,
3233 NODE_FLAGS_PERMANENTLY_DISABLED,
3235 true /* set_flag*/);
3238 /* Administratively re-enable a node */
3239 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3243 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3244 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3248 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3250 return update_flags_and_ipreallocate(ctdb, NULL,
3251 update_flags_not_disabled,
3252 NODE_FLAGS_PERMANENTLY_DISABLED,
3254 false /* set_flag*/);
3258 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3262 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3267 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3269 return update_flags_and_ipreallocate(ctdb, NULL,
3270 update_flags_stopped,
3273 true /* set_flag*/);
3276 /* Continue a stopped node */
3277 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3281 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3286 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3288 return update_flags_and_ipreallocate(ctdb, NULL,
3289 update_flags_not_stopped,
3292 false /* set_flag */);
3295 static uint32_t get_generation(struct ctdb_context *ctdb)
3297 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3298 struct ctdb_vnn_map *vnnmap=NULL;
3300 uint32_t generation;
3302 /* wait until the recmaster is not in recovery mode */
3304 uint32_t recmode, recmaster;
3306 if (vnnmap != NULL) {
3307 talloc_free(vnnmap);
3311 /* get the recmaster */
3312 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3314 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3315 talloc_free(tmp_ctx);
3319 /* get recovery mode */
3320 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3322 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3323 talloc_free(tmp_ctx);
3327 /* get the current generation number */
3328 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3330 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3331 talloc_free(tmp_ctx);
3335 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3336 generation = vnnmap->generation;
3337 talloc_free(tmp_ctx);
3345 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3347 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3350 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3355 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3357 struct ctdb_ban_time bantime;
3363 bantime.pnn = options.pnn;
3364 bantime.time = strtoul(argv[0], NULL, 0);
3366 if (bantime.time == 0) {
3367 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3371 return update_flags_and_ipreallocate(ctdb, &bantime,
3372 update_state_banned,
3375 true /* set_flag*/);
3380 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3382 struct ctdb_ban_time bantime;
3384 bantime.pnn = options.pnn;
3387 return update_flags_and_ipreallocate(ctdb, &bantime,
3388 update_state_banned,
3391 false /* set_flag*/);
3395 show ban information for a node
3397 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3400 struct ctdb_node_map *nodemap=NULL;
3401 struct ctdb_ban_time *bantime;
3403 /* verify the node exists */
3404 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3406 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3410 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3412 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3416 if (bantime->time == 0) {
3417 printf("Node %u is not banned\n", bantime->pnn);
3419 printf("Node %u is banned, %d seconds remaining\n",
3420 bantime->pnn, bantime->time);
3429 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3433 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3435 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3445 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3448 uint32_t generation, next_generation;
3451 /* "force" option ignores freeze failure and forces recovery */
3452 force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3454 /* record the current generation number */
3455 generation = get_generation(ctdb);
3457 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3460 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3463 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3466 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3468 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3472 /* wait until we are in a new generation */
3474 next_generation = get_generation(ctdb);
3475 if (next_generation != generation) {
3486 display monitoring mode of a remote node
3488 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3493 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3495 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3498 if (!options.machinereadable){
3499 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3502 printf(":%d:\n",monmode);
3509 display capabilities of a remote node
3511 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3513 uint32_t capabilities;
3516 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3518 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3522 if (!options.machinereadable){
3523 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3524 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3525 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3526 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3528 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3529 printf(":%d:%d:%d:%d:\n",
3530 !!(capabilities&CTDB_CAP_RECMASTER),
3531 !!(capabilities&CTDB_CAP_LMASTER),
3532 !!(capabilities&CTDB_CAP_LVS),
3533 !!(capabilities&CTDB_CAP_NATGW));
3539 display lvs configuration
3542 static uint32_t lvs_exclude_flags[] = {
3543 /* Look for a nice healthy node */
3544 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3545 /* If not found, an UNHEALTHY node will do */
3546 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3550 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3552 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3553 struct ctdb_node_map *orig_nodemap=NULL;
3554 struct ctdb_node_map *nodemap;
3557 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3558 tmp_ctx, &orig_nodemap);
3560 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3561 talloc_free(tmp_ctx);
3565 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3567 if (nodemap == NULL) {
3575 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3576 struct ctdb_node_map *t =
3577 filter_nodemap_by_flags(ctdb, nodemap,
3578 lvs_exclude_flags[i]);
3585 /* At least 1 node without excluded flags */
3587 for (j = 0; j < t->num; j++) {
3588 printf("%d:%s\n", t->nodes[j].pnn,
3589 ctdb_addr_to_str(&t->nodes[j].addr));
3596 talloc_free(tmp_ctx);
3601 display who is the lvs master
3603 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3605 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3606 struct ctdb_node_map *orig_nodemap=NULL;
3607 struct ctdb_node_map *nodemap;
3610 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3611 tmp_ctx, &orig_nodemap);
3613 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3614 talloc_free(tmp_ctx);
3618 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3620 if (nodemap == NULL) {
3626 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3627 struct ctdb_node_map *t =
3628 filter_nodemap_by_flags(ctdb, nodemap,
3629 lvs_exclude_flags[i]);
3637 printf(options.machinereadable ?
3638 "%d\n" : "Node %d is LVS master\n",
3645 printf("There is no LVS master\n");
3648 talloc_free(tmp_ctx);
3653 disable monitoring on a node
3655 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3660 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3662 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3665 printf("Monitoring mode:%s\n","DISABLED");
3671 enable monitoring on a node
3673 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3678 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3680 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3683 printf("Monitoring mode:%s\n","ACTIVE");
3689 display remote list of keys/data for a db
3691 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3693 const char *db_name;
3694 struct ctdb_db_context *ctdb_db;
3696 struct ctdb_dump_db_context c;
3703 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3707 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3708 if (ctdb_db == NULL) {
3709 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3713 if (options.printlmaster) {
3714 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3715 ctdb, &ctdb->vnn_map);
3717 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3725 c.printemptyrecords = (bool)options.printemptyrecords;
3726 c.printdatasize = (bool)options.printdatasize;
3727 c.printlmaster = (bool)options.printlmaster;
3728 c.printhash = (bool)options.printhash;
3729 c.printrecordflags = (bool)options.printrecordflags;
3731 /* traverse and dump the cluster tdb */
3732 ret = ctdb_dump_db(ctdb_db, &c);
3734 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3735 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3736 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3740 talloc_free(ctdb_db);
3742 printf("Dumped %d records\n", ret);
3746 struct cattdb_data {
3747 struct ctdb_context *ctdb;
3751 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3753 struct cattdb_data *d = private_data;
3754 struct ctdb_dump_db_context c;
3760 c.printemptyrecords = (bool)options.printemptyrecords;
3761 c.printdatasize = (bool)options.printdatasize;
3762 c.printlmaster = false;
3763 c.printhash = (bool)options.printhash;
3764 c.printrecordflags = true;
3766 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3770 cat the local tdb database using same format as catdb
3772 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3774 const char *db_name;
3775 struct ctdb_db_context *ctdb_db;
3776 struct cattdb_data d;
3783 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3787 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3788 if (ctdb_db == NULL) {
3789 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3793 /* traverse the local tdb */
3796 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3797 printf("Failed to cattdb data\n");
3800 talloc_free(ctdb_db);
3802 printf("Dumped %d records\n", d.count);
3807 display the content of a database key
3809 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3811 const char *db_name;
3812 struct ctdb_db_context *ctdb_db;
3813 struct ctdb_record_handle *h;
3814 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3822 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3826 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3827 if (ctdb_db == NULL) {
3828 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3832 key.dptr = discard_const(argv[1]);
3833 key.dsize = strlen((char *)key.dptr);
3835 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3837 printf("Failed to fetch record '%s' on node %d\n",
3838 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3839 talloc_free(tmp_ctx);
3843 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3845 talloc_free(ctdb_db);
3846 talloc_free(tmp_ctx);
3851 display the content of a database key
3853 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3855 const char *db_name;
3856 struct ctdb_db_context *ctdb_db;
3857 struct ctdb_record_handle *h;
3858 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3866 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3870 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3871 if (ctdb_db == NULL) {
3872 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3876 key.dptr = discard_const(argv[1]);
3877 key.dsize = strlen((char *)key.dptr);
3879 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3881 printf("Failed to fetch record '%s' on node %d\n",
3882 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3883 talloc_free(tmp_ctx);
3887 data.dptr = discard_const(argv[2]);
3888 data.dsize = strlen((char *)data.dptr);
3890 if (ctdb_record_store(h, data) != 0) {
3891 printf("Failed to store record\n");
3895 talloc_free(ctdb_db);
3896 talloc_free(tmp_ctx);
3901 fetch a record from a persistent database
3903 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3905 const char *db_name;
3906 struct ctdb_db_context *ctdb_db;
3907 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3908 struct ctdb_transaction_handle *h;
3915 talloc_free(tmp_ctx);
3919 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3920 talloc_free(tmp_ctx);
3924 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3926 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3927 talloc_free(tmp_ctx);
3931 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3932 if (ctdb_db == NULL) {
3933 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3934 talloc_free(tmp_ctx);
3938 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3940 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3941 talloc_free(tmp_ctx);
3945 key.dptr = discard_const(argv[1]);
3946 key.dsize = strlen(argv[1]);
3947 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3949 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3950 talloc_free(tmp_ctx);
3954 if (data.dsize == 0 || data.dptr == NULL) {
3955 DEBUG(DEBUG_ERR,("Record is empty\n"));
3956 talloc_free(tmp_ctx);
3961 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3963 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3964 talloc_free(tmp_ctx);
3967 write(fd, data.dptr, data.dsize);
3970 write(1, data.dptr, data.dsize);
3973 /* abort the transaction */
3977 talloc_free(tmp_ctx);
3982 fetch a record from a tdb-file
3984 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3986 const char *tdb_file;
3989 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3998 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4000 printf("Failed to open TDB file %s\n", tdb_file);
4004 if (!strncmp(argv[1], "0x", 2)) {
4005 key = hextodata(tmp_ctx, argv[1] + 2);
4006 if (key.dsize == 0) {
4007 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4011 key.dptr = discard_const(argv[1]);
4012 key.dsize = strlen(argv[1]);
4015 data = tdb_fetch(tdb, key);
4016 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4017 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4025 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4027 printf("Failed to open output file %s\n", argv[2]);
4030 if (options.verbose){
4031 write(fd, data.dptr, data.dsize);
4033 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4037 if (options.verbose){
4038 write(1, data.dptr, data.dsize);
4040 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4044 talloc_free(tmp_ctx);
4049 store a record and header to a tdb-file
4051 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4053 const char *tdb_file;
4055 TDB_DATA key, value, data;
4056 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4057 struct ctdb_ltdb_header header;
4065 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4067 printf("Failed to open TDB file %s\n", tdb_file);
4071 if (!strncmp(argv[1], "0x", 2)) {
4072 key = hextodata(tmp_ctx, argv[1] + 2);
4073 if (key.dsize == 0) {
4074 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4078 key.dptr = discard_const(argv[1]);
4079 key.dsize = strlen(argv[1]);
4082 if (!strncmp(argv[2], "0x", 2)) {
4083 value = hextodata(tmp_ctx, argv[2] + 2);
4084 if (value.dsize == 0) {
4085 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4089 value.dptr = discard_const(argv[2]);
4090 value.dsize = strlen(argv[2]);
4093 ZERO_STRUCT(header);
4095 header.rsn = atoll(argv[3]);
4098 header.dmaster = atoi(argv[4]);
4101 header.flags = atoi(argv[5]);
4104 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4105 data.dptr = talloc_size(tmp_ctx, data.dsize);
4106 if (data.dptr == NULL) {
4107 printf("Failed to allocate header+value\n");
4111 *(struct ctdb_ltdb_header *)data.dptr = header;
4112 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4114 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4115 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4122 talloc_free(tmp_ctx);
4127 write a record to a persistent database
4129 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4131 const char *db_name;
4132 struct ctdb_db_context *ctdb_db;
4133 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4134 struct ctdb_transaction_handle *h;
4140 talloc_free(tmp_ctx);
4144 fd = open(argv[2], O_RDONLY);
4146 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4147 talloc_free(tmp_ctx);
4151 ret = fstat(fd, &st);
4153 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4155 talloc_free(tmp_ctx);
4159 if (!S_ISREG(st.st_mode)) {
4160 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4162 talloc_free(tmp_ctx);
4166 data.dsize = st.st_size;
4167 if (data.dsize == 0) {
4170 data.dptr = talloc_size(tmp_ctx, data.dsize);
4171 if (data.dptr == NULL) {
4172 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4174 talloc_free(tmp_ctx);
4177 ret = read(fd, data.dptr, data.dsize);
4178 if (ret != data.dsize) {
4179 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4181 talloc_free(tmp_ctx);
4190 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4191 if (ctdb_db == NULL) {
4192 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4193 talloc_free(tmp_ctx);
4197 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4199 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4200 talloc_free(tmp_ctx);
4204 key.dptr = discard_const(argv[1]);
4205 key.dsize = strlen(argv[1]);
4206 ret = ctdb_transaction_store(h, key, data);
4208 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4209 talloc_free(tmp_ctx);
4213 ret = ctdb_transaction_commit(h);
4215 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4216 talloc_free(tmp_ctx);
4221 talloc_free(tmp_ctx);
4226 * delete a record from a persistent database
4228 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4230 const char *db_name;
4231 struct ctdb_db_context *ctdb_db;
4232 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4233 struct ctdb_transaction_handle *h;
4240 talloc_free(tmp_ctx);
4244 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4245 talloc_free(tmp_ctx);
4249 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4251 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4252 talloc_free(tmp_ctx);
4256 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4257 if (ctdb_db == NULL) {
4258 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4259 talloc_free(tmp_ctx);
4263 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4265 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4266 talloc_free(tmp_ctx);
4270 key.dptr = discard_const(argv[1]);
4271 key.dsize = strlen(argv[1]);
4272 ret = ctdb_transaction_store(h, key, tdb_null);
4274 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4275 talloc_free(tmp_ctx);
4279 ret = ctdb_transaction_commit(h);
4281 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4282 talloc_free(tmp_ctx);
4286 talloc_free(tmp_ctx);
4290 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4295 const char *ret; /* Next byte after successfully parsed value */
4297 /* Error, unless someone says otherwise */
4299 /* Indicates no value to parse */
4302 /* Skip whitespace */
4303 n = strspn(s, " \t");
4307 /* Quoted ASCII string - no wide characters! */
4309 n = strcspn(t, "\"");
4313 data->dptr = talloc_memdup(mem_ctx, t, n);
4314 CTDB_NOMEM_ABORT(data->dptr);
4318 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4321 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4327 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4328 TDB_DATA *key, TDB_DATA *value)
4330 char line [1024]; /* FIXME: make this more flexible? */
4334 ptr = fgets(line, sizeof(line), file);
4341 t = ptrans_parse_string(mem_ctx, line, key);
4342 if (t == NULL || key->dptr == NULL) {
4343 /* Line Ignored but not EOF */
4348 t = ptrans_parse_string(mem_ctx, t, value);
4350 /* Line Ignored but not EOF */
4351 talloc_free(key->dptr);
4360 * Update a persistent database as per file/stdin
4362 static int control_ptrans(struct ctdb_context *ctdb,
4363 int argc, const char **argv)
4365 const char *db_name;
4366 struct ctdb_db_context *ctdb_db;
4367 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4368 struct ctdb_transaction_handle *h;
4369 TDB_DATA key, value;
4374 talloc_free(tmp_ctx);
4380 file = fopen(argv[1], "r");
4382 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4383 talloc_free(tmp_ctx);
4390 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4391 if (ctdb_db == NULL) {
4392 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4396 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4398 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4402 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4403 if (key.dsize != 0) {
4404 ret = ctdb_transaction_store(h, key, value);
4405 /* Minimise memory use */
4406 talloc_free(key.dptr);
4407 if (value.dptr != NULL) {
4408 talloc_free(value.dptr);
4411 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4412 ctdb_transaction_cancel(h);
4418 ret = ctdb_transaction_commit(h);
4420 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4424 if (file != stdin) {
4427 talloc_free(tmp_ctx);
4431 if (file != stdin) {
4435 talloc_free(tmp_ctx);
4440 check if a service is bound to a port or not
4442 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4447 struct sockaddr_in sin;
4450 printf("Use: ctdb chktcport <port>\n");
4454 port = atoi(argv[0]);
4456 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4458 printf("Failed to open local socket\n");
4462 v = fcntl(s, F_GETFL, 0);
4463 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4464 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4467 bzero(&sin, sizeof(sin));
4468 sin.sin_family = PF_INET;
4469 sin.sin_port = htons(port);
4470 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4473 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4482 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4483 TDB_DATA data, void *private_data)
4485 DEBUG(DEBUG_ERR,("Log data received\n"));
4486 if (data.dsize > 0) {
4487 printf("%s", data.dptr);
4494 display a list of log messages from the in memory ringbuffer
4496 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4500 struct ctdb_get_log_addr log_addr;
4504 /* Process options */
4506 log_addr.pnn = ctdb_get_pnn(ctdb);
4507 log_addr.level = DEBUG_NOTICE;
4508 for (i = 0; i < argc; i++) {
4509 if (strcmp(argv[i], "recoverd") == 0) {
4510 main_daemon = false;
4512 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4513 log_addr.level = get_debug_by_desc(argv[i]);
4515 log_addr.level = strtol(argv[i], NULL, 0);
4520 /* Our message port is our PID */
4521 log_addr.srvid = getpid();
4523 data.dptr = (unsigned char *)&log_addr;
4524 data.dsize = sizeof(log_addr);
4526 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4528 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4531 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4536 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4538 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4539 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4540 if (ret != 0 || res != 0) {
4541 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4542 talloc_free(tmp_ctx);
4545 talloc_free(tmp_ctx);
4547 ret = ctdb_client_send_message(ctdb, options.pnn,
4548 CTDB_SRVID_GETLOG, data);
4550 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4555 tv = timeval_current();
4556 /* this loop will terminate when we have received the reply */
4557 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4558 event_loop_once(ctdb->ev);
4561 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4567 clear the in memory log area
4569 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4573 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4574 /* "recoverd" not given - get logs from main daemon */
4577 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4579 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4580 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4581 if (ret != 0 || res != 0) {
4582 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4583 talloc_free(tmp_ctx);
4587 talloc_free(tmp_ctx);
4589 TDB_DATA data; /* unused in recoverd... */
4592 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4594 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4602 /* Reload public IPs on a specified nodes */
4603 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4605 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4611 assert_single_node_only();
4617 /* Determine the nodes where IPs need to be reloaded */
4618 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4619 options.pnn, true, &nodes, &pnn_mode)) {
4625 /* Disable takeover runs on all connected nodes. A reply
4626 * indicating success is needed from each node so all nodes
4627 * will need to be active. This will retry until maxruntime
4628 * is exceeded, hence no error handling.
4630 * A check could be added to not allow reloading of IPs when
4631 * there are disconnected nodes. However, this should
4632 * probably be left up to the administrator.
4634 timeout = LONGTIMEOUT;
4635 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4636 "Disable takeover runs", true);
4638 /* Now tell all the desired nodes to reload their public IPs.
4639 * Keep trying this until it succeeds. This assumes all
4640 * failures are transient, which might not be true...
4642 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4643 nodes, 0, LONGTIMELIMIT(),
4645 NULL, NULL, NULL) != 0) {
4647 ("Unable to reload IPs on some nodes, try again.\n"));
4651 /* It isn't strictly necessary to wait until takeover runs are
4652 * re-enabled but doing so can't hurt.
4655 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4656 "Enable takeover runs", true);
4662 talloc_free(tmp_ctx);
4667 display a list of the databases on a remote ctdb
4669 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4672 struct ctdb_dbid_map *dbmap=NULL;
4674 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4676 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4680 if(options.machinereadable){
4681 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4682 for(i=0;i<dbmap->num;i++){
4690 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4691 dbmap->dbs[i].dbid, ctdb, &path);
4692 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4693 dbmap->dbs[i].dbid, ctdb, &name);
4694 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4695 dbmap->dbs[i].dbid, ctdb, &health);
4696 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4697 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4698 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4699 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4700 dbmap->dbs[i].dbid, name, path,
4701 !!(persistent), !!(sticky),
4702 !!(health), !!(readonly));
4707 printf("Number of databases:%d\n", dbmap->num);
4708 for(i=0;i<dbmap->num;i++){
4716 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4717 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4718 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4719 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4720 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4721 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4722 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4723 dbmap->dbs[i].dbid, name, path,
4724 persistent?" PERSISTENT":"",
4725 sticky?" STICKY":"",
4726 readonly?" READONLY":"",
4727 health?" UNHEALTHY":"");
4734 display the status of a database on a remote ctdb
4736 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4738 const char *db_name;
4748 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4752 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4753 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4754 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4755 db_id, db_name, path,
4756 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4757 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4758 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4759 (health ? health : "OK"));
4765 check if the local node is recmaster or not
4766 it will return 1 if this node is the recmaster and 0 if it is not
4767 or if the local ctdb daemon could not be contacted
4769 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4771 uint32_t mypnn, recmaster;
4774 assert_single_node_only();
4776 mypnn = getpnn(ctdb);
4778 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4780 printf("Failed to get the recmaster\n");
4784 if (recmaster != mypnn) {
4785 printf("this node is not the recmaster\n");
4789 printf("this node is the recmaster\n");
4796 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4799 struct timeval tv = timeval_current();
4800 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4802 printf("Unable to get ping response from node %u\n", options.pnn);
4805 printf("response from %u time=%.6f sec (%d clients)\n",
4806 options.pnn, timeval_elapsed(&tv), ret);
4813 get a node's runstate
4815 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4818 enum ctdb_runstate runstate;
4820 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4822 printf("Unable to get runstate response from node %u\n",
4827 enum ctdb_runstate t;
4829 for (i=0; i<argc; i++) {
4831 t = runstate_from_string(argv[i]);
4832 if (t == CTDB_RUNSTATE_UNKNOWN) {
4833 printf("Invalid run state (%s)\n", argv[i]);
4837 if (t == runstate) {
4844 printf("CTDB not in required run state (got %s)\n",
4845 runstate_to_string((enum ctdb_runstate)runstate));
4850 printf("%s\n", runstate_to_string(runstate));
4858 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4869 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4871 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4875 printf("%-23s = %u\n", name, value);
4882 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4893 value = strtoul(argv[1], NULL, 0);
4895 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4897 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4906 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4912 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4914 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4918 for (i=0;i<count;i++) {
4919 control_getvar(ctdb, 1, &list[i]);
4928 display debug level on a node
4930 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4935 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4937 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4940 if (options.machinereadable){
4941 printf(":Name:Level:\n");
4942 printf(":%s:%d:\n",get_debug_by_level(level),level);
4944 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4951 display reclock file of a node
4953 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4956 const char *reclock;
4958 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4960 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4963 if (options.machinereadable){
4964 if (reclock != NULL) {
4965 printf("%s", reclock);
4968 if (reclock == NULL) {
4969 printf("No reclock file used.\n");
4971 printf("Reclock file:%s\n", reclock);
4979 set the reclock file of a node
4981 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4984 const char *reclock;
4988 } else if (argc == 1) {
4994 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4996 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
5003 set the natgw state on/off
5005 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
5008 uint32_t natgwstate;
5014 if (!strcmp(argv[0], "on")) {
5016 } else if (!strcmp(argv[0], "off")) {
5022 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
5024 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
5032 set the lmaster role on/off
5034 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5037 uint32_t lmasterrole;
5043 if (!strcmp(argv[0], "on")) {
5045 } else if (!strcmp(argv[0], "off")) {
5051 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
5053 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
5061 set the recmaster role on/off
5063 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5066 uint32_t recmasterrole;
5072 if (!strcmp(argv[0], "on")) {
5074 } else if (!strcmp(argv[0], "off")) {
5080 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5082 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5090 set debug level on a node or all nodes
5092 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5098 printf("You must specify the debug level. Valid levels are:\n");
5099 for (i=0; debug_levels[i].description != NULL; i++) {
5100 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5106 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
5107 level = get_debug_by_desc(argv[0]);
5109 level = strtol(argv[0], NULL, 0);
5112 for (i=0; debug_levels[i].description != NULL; i++) {
5113 if (level == debug_levels[i].level) {
5117 if (debug_levels[i].description == NULL) {
5118 printf("Invalid debug level, must be one of\n");
5119 for (i=0; debug_levels[i].description != NULL; i++) {
5120 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5125 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5127 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5136 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5142 priority = strtol(argv[0], NULL, 0);
5146 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5148 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5150 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5157 attach to a database
5159 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5161 const char *db_name;
5162 struct ctdb_db_context *ctdb_db;
5163 bool persistent = false;
5173 if (strcmp(argv[1], "persistent") != 0) {
5179 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5180 if (ctdb_db == NULL) {
5181 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5189 * detach from a database
5191 static int control_detach(struct ctdb_context *ctdb, int argc,
5196 int ret, i, status = 0;
5197 struct ctdb_node_map *nodemap = NULL;
5198 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5205 assert_single_node_only();
5207 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5210 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5211 "when recovery is active\n"));
5212 talloc_free(tmp_ctx);
5216 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5219 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5221 talloc_free(tmp_ctx);
5225 for (i=0; i<nodemap->num; i++) {
5228 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5232 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5236 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5237 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5238 "inactive (stopped or banned) node "
5239 "%u\n", nodemap->nodes[i].pnn));
5240 talloc_free(tmp_ctx);
5244 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5245 nodemap->nodes[i].pnn,
5246 "AllowClientDBAttach",
5249 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5250 "AllowClientDBAttach from node %u\n",
5251 nodemap->nodes[i].pnn));
5252 talloc_free(tmp_ctx);
5257 DEBUG(DEBUG_ERR, ("Database access is still active on "
5258 "node %u. Set AllowClientDBAttach=0 "
5260 nodemap->nodes[i].pnn));
5261 talloc_free(tmp_ctx);
5266 talloc_free(tmp_ctx);
5268 for (i=0; i<argc; i++) {
5269 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5273 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5274 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5275 "cannot be detached\n", argv[i]));
5280 ret = ctdb_detach(ctdb, db_id);
5282 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5294 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5296 struct ctdb_db_priority db_prio;
5303 db_prio.db_id = strtoul(argv[0], NULL, 0);
5304 db_prio.priority = strtoul(argv[1], NULL, 0);
5306 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5308 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5318 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5320 uint32_t db_id, priority;
5327 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5331 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5333 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5337 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5343 set the sticky records capability for a database
5345 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5347 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5355 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5359 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5361 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5362 talloc_free(tmp_ctx);
5366 talloc_free(tmp_ctx);
5371 set the readonly capability for a database
5373 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5375 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5383 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5387 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5389 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5390 talloc_free(tmp_ctx);
5394 talloc_free(tmp_ctx);
5401 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5411 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5415 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5417 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5421 printf("Sequence number:%lld\n", (long long)seqnum);
5427 run an eventscript on a node
5429 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5435 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5438 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5442 data.dptr = (unsigned char *)discard_const(argv[0]);
5443 data.dsize = strlen((char *)data.dptr) + 1;
5445 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5447 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5448 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5449 if (ret != 0 || res != 0) {
5450 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5451 talloc_free(tmp_ctx);
5454 talloc_free(tmp_ctx);
5458 #define DB_VERSION 1
5459 #define MAX_DB_NAME 64
5460 struct db_file_header {
5461 unsigned long version;
5463 unsigned long persistent;
5465 const char name[MAX_DB_NAME];
5468 struct backup_data {
5469 struct ctdb_marshall_buffer *records;
5472 bool traverse_error;
5475 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5477 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5478 struct ctdb_rec_data *rec;
5480 /* add the record */
5481 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5483 bd->traverse_error = true;
5484 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5487 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5488 if (bd->records == NULL) {
5489 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5490 bd->traverse_error = true;
5493 bd->records->count++;
5494 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5495 bd->len += rec->length;
5503 * backup a database to a file
5505 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5507 const char *db_name;
5509 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5510 struct db_file_header dbhdr;
5511 struct ctdb_db_context *ctdb_db;
5512 struct backup_data *bd;
5515 const char *reason = NULL;
5519 assert_single_node_only();
5522 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5526 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5530 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5531 db_id, tmp_ctx, &reason);
5533 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5535 talloc_free(tmp_ctx);
5539 uint32_t allow_unhealthy = 0;
5541 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5542 "AllowUnhealthyDBRead",
5545 if (allow_unhealthy != 1) {
5546 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5549 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5551 talloc_free(tmp_ctx);
5555 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5557 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5558 "tunnable AllowUnhealthyDBRead = %u\n",
5562 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5563 if (ctdb_db == NULL) {
5564 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5565 talloc_free(tmp_ctx);
5570 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5572 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5573 talloc_free(tmp_ctx);
5578 bd = talloc_zero(tmp_ctx, struct backup_data);
5580 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5581 talloc_free(tmp_ctx);
5585 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5586 if (bd->records == NULL) {
5587 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5588 talloc_free(tmp_ctx);
5592 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5593 bd->records->db_id = ctdb_db->db_id;
5594 /* traverse the database collecting all records */
5595 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5596 bd->traverse_error) {
5597 DEBUG(DEBUG_ERR,("Traverse error\n"));
5598 talloc_free(tmp_ctx);
5602 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5605 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5607 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5608 talloc_free(tmp_ctx);
5613 dbhdr.version = DB_VERSION;
5614 dbhdr.timestamp = time(NULL);
5615 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5616 dbhdr.size = bd->len;
5617 if (strlen(argv[0]) >= MAX_DB_NAME) {
5618 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5621 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5622 ret = write(fh, &dbhdr, sizeof(dbhdr));
5624 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5627 ret = write(fh, bd->records, bd->len);
5629 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5638 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5642 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5644 talloc_free(tmp_ctx);
5649 * restore a database from a file
5651 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5654 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5657 struct db_file_header dbhdr;
5658 struct ctdb_db_context *ctdb_db;
5659 struct ctdb_node_map *nodemap=NULL;
5660 struct ctdb_vnn_map *vnnmap=NULL;
5662 struct ctdb_control_wipe_database w;
5664 uint32_t generation;
5669 assert_single_node_only();
5671 if (argc < 1 || argc > 2) {
5672 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5676 fh = open(argv[0], O_RDONLY);
5678 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5679 talloc_free(tmp_ctx);
5683 read(fh, &dbhdr, sizeof(dbhdr));
5684 if (dbhdr.version != DB_VERSION) {
5685 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5687 talloc_free(tmp_ctx);
5691 dbname = discard_const(dbhdr.name);
5693 dbname = discard_const(argv[1]);
5696 outdata.dsize = dbhdr.size;
5697 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5698 if (outdata.dptr == NULL) {
5699 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5701 talloc_free(tmp_ctx);
5704 read(fh, outdata.dptr, outdata.dsize);
5707 tm = localtime(&dbhdr.timestamp);
5708 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5709 printf("Restoring database '%s' from backup @ %s\n",
5713 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5714 if (ctdb_db == NULL) {
5715 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5716 talloc_free(tmp_ctx);
5720 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5722 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5723 talloc_free(tmp_ctx);
5728 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5730 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5731 talloc_free(tmp_ctx);
5735 /* freeze all nodes */
5736 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5737 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5738 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5744 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5745 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5746 talloc_free(tmp_ctx);
5751 generation = vnnmap->generation;
5752 data.dptr = (void *)&generation;
5753 data.dsize = sizeof(generation);
5755 /* start a cluster wide transaction */
5756 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5757 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5759 TIMELIMIT(), false, data,
5762 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5767 w.db_id = ctdb_db->db_id;
5768 w.transaction_id = generation;
5770 data.dptr = (void *)&w;
5771 data.dsize = sizeof(w);
5773 /* wipe all the remote databases. */
5774 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5775 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5777 TIMELIMIT(), false, data,
5780 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5781 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5782 talloc_free(tmp_ctx);
5786 /* push the database */
5787 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5788 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5790 TIMELIMIT(), false, outdata,
5793 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5794 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5795 talloc_free(tmp_ctx);
5799 data.dptr = (void *)&ctdb_db->db_id;
5800 data.dsize = sizeof(ctdb_db->db_id);
5802 /* mark the database as healthy */
5803 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5804 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5806 TIMELIMIT(), false, data,
5809 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5810 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5811 talloc_free(tmp_ctx);
5815 data.dptr = (void *)&generation;
5816 data.dsize = sizeof(generation);
5818 /* commit all the changes */
5819 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5821 TIMELIMIT(), false, data,
5824 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5825 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5826 talloc_free(tmp_ctx);
5831 /* thaw all nodes */
5832 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5833 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5839 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5840 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5841 talloc_free(tmp_ctx);
5846 talloc_free(tmp_ctx);
5851 * dump a database backup from a file
5853 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5855 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5857 struct db_file_header dbhdr;
5861 struct ctdb_rec_data *rec = NULL;
5862 struct ctdb_marshall_buffer *m;
5863 struct ctdb_dump_db_context c;
5865 assert_single_node_only();
5868 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5872 fh = open(argv[0], O_RDONLY);
5874 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5875 talloc_free(tmp_ctx);
5879 read(fh, &dbhdr, sizeof(dbhdr));
5880 if (dbhdr.version != DB_VERSION) {
5881 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5883 talloc_free(tmp_ctx);
5887 outdata.dsize = dbhdr.size;
5888 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5889 if (outdata.dptr == NULL) {
5890 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5892 talloc_free(tmp_ctx);
5895 read(fh, outdata.dptr, outdata.dsize);
5897 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5899 tm = localtime(&dbhdr.timestamp);
5900 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5901 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5902 dbhdr.name, m->db_id, tbuf);
5906 c.printemptyrecords = (bool)options.printemptyrecords;
5907 c.printdatasize = (bool)options.printdatasize;
5908 c.printlmaster = false;
5909 c.printhash = (bool)options.printhash;
5910 c.printrecordflags = (bool)options.printrecordflags;
5912 for (i=0; i < m->count; i++) {
5916 /* we do not want the header splitted, so we pass NULL*/
5917 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5920 ctdb_dumpdb_record(ctdb, key, data, &c);
5923 printf("Dumped %d records\n", i);
5924 talloc_free(tmp_ctx);
5929 * wipe a database from a file
5931 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5934 const char *db_name;
5936 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5938 struct ctdb_db_context *ctdb_db;
5939 struct ctdb_node_map *nodemap = NULL;
5940 struct ctdb_vnn_map *vnnmap = NULL;
5942 struct ctdb_control_wipe_database w;
5944 uint32_t generation;
5947 assert_single_node_only();
5950 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5954 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5958 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5959 if (ctdb_db == NULL) {
5960 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5962 talloc_free(tmp_ctx);
5966 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5969 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5971 talloc_free(tmp_ctx);
5975 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5978 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5980 talloc_free(tmp_ctx);
5984 /* freeze all nodes */
5985 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5986 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5987 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5994 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5995 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5996 CTDB_RECOVERY_ACTIVE);
5997 talloc_free(tmp_ctx);
6002 generation = vnnmap->generation;
6003 data.dptr = (void *)&generation;
6004 data.dsize = sizeof(generation);
6006 /* start a cluster wide transaction */
6007 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6008 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
6010 TIMELIMIT(), false, data,
6014 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
6015 "transactions.\n"));
6019 w.db_id = ctdb_db->db_id;
6020 w.transaction_id = generation;
6022 data.dptr = (void *)&w;
6023 data.dsize = sizeof(w);
6025 /* wipe all the remote databases. */
6026 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6027 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
6029 TIMELIMIT(), false, data,
6032 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
6033 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6034 talloc_free(tmp_ctx);
6038 data.dptr = (void *)&ctdb_db->db_id;
6039 data.dsize = sizeof(ctdb_db->db_id);
6041 /* mark the database as healthy */
6042 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6043 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
6045 TIMELIMIT(), false, data,
6048 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
6049 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6050 talloc_free(tmp_ctx);
6054 data.dptr = (void *)&generation;
6055 data.dsize = sizeof(generation);
6057 /* commit all the changes */
6058 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
6060 TIMELIMIT(), false, data,
6063 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
6064 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6065 talloc_free(tmp_ctx);
6069 /* thaw all nodes */
6070 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6071 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
6077 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
6078 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6079 talloc_free(tmp_ctx);
6083 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
6085 talloc_free(tmp_ctx);
6092 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6098 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6099 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6100 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6101 if (ret != 0 || res != 0) {
6102 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6103 talloc_free(tmp_ctx);
6106 write(1, data.dptr, data.dsize);
6107 talloc_free(tmp_ctx);
6112 handler for memory dumps
6114 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6115 TDB_DATA data, void *private_data)
6117 write(1, data.dptr, data.dsize);
6122 dump memory usage on the recovery daemon
6124 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6128 struct srvid_request rd;
6130 rd.pnn = ctdb_get_pnn(ctdb);
6131 rd.srvid = getpid();
6133 /* register a message port for receiveing the reply so that we
6134 can receive the reply
6136 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6139 data.dptr = (uint8_t *)&rd;
6140 data.dsize = sizeof(rd);
6142 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6144 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6148 /* this loop will terminate when we have received the reply */
6150 event_loop_once(ctdb->ev);
6157 send a message to a srvid
6159 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6161 unsigned long srvid;
6169 srvid = strtoul(argv[0], NULL, 0);
6171 data.dptr = (uint8_t *)discard_const(argv[1]);
6172 data.dsize= strlen(argv[1]);
6174 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6176 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6184 handler for msglisten
6186 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6187 TDB_DATA data, void *private_data)
6191 printf("Message received: ");
6192 for (i=0;i<data.dsize;i++) {
6193 printf("%c", data.dptr[i]);
6199 listen for messages on a messageport
6201 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6207 /* register a message port and listen for messages
6209 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6210 printf("Listening for messages on srvid:%d\n", (int)srvid);
6213 event_loop_once(ctdb->ev);
6220 list all nodes in the cluster
6221 we parse the nodes file directly
6223 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6225 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6226 struct pnn_node *pnn_nodes;
6227 struct pnn_node *pnn_node;
6229 assert_single_node_only();
6231 pnn_nodes = read_nodes_file(mem_ctx);
6232 if (pnn_nodes == NULL) {
6233 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6234 talloc_free(mem_ctx);
6238 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6239 const char *addr = ctdb_addr_to_str(&pnn_node->addr);
6240 if (options.machinereadable){
6241 printf(":%d:%s:\n", pnn_node->pnn, addr);
6243 printf("%s\n", addr);
6246 talloc_free(mem_ctx);
6252 reload the nodes file on the local node
6254 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6258 struct ctdb_node_map *nodemap=NULL;
6260 assert_single_node_only();
6262 mypnn = ctdb_get_pnn(ctdb);
6264 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6266 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6270 /* reload the nodes file on all remote nodes */
6271 for (i=0;i<nodemap->num;i++) {
6272 if (nodemap->nodes[i].pnn == mypnn) {
6275 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6276 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6277 nodemap->nodes[i].pnn);
6279 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6283 /* reload the nodes file on the local node */
6284 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6285 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6287 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6290 /* initiate a recovery */
6291 control_recover(ctdb, argc, argv);
6297 static const struct {
6299 int (*fn)(struct ctdb_context *, int, const char **);
6301 bool without_daemon; /* can be run without daemon running ? */
6304 } ctdb_commands[] = {
6305 { "version", control_version, true, true, "show version of ctdb" },
6306 { "status", control_status, true, false, "show node status" },
6307 { "uptime", control_uptime, true, false, "show node uptime" },
6308 { "ping", control_ping, true, false, "ping all nodes" },
6309 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6310 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6311 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6312 { "listvars", control_listvars, true, false, "list tunable variables"},
6313 { "statistics", control_statistics, false, false, "show statistics" },
6314 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6315 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6316 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6317 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6318 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6319 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6320 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6321 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6322 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6323 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6324 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6325 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6326 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6327 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6328 { "lvs", control_lvs, true, false, "show lvs configuration" },
6329 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6330 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6331 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6332 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6333 { "getdebug", control_getdebug, true, false, "get debug level" },
6334 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6335 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6336 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6337 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6338 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6339 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6340 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6341 { "disable", control_disable, true, false, "disable a nodes public IP" },
6342 { "enable", control_enable, true, false, "enable a nodes public IP" },
6343 { "stop", control_stop, true, false, "stop a node" },
6344 { "continue", control_continue, true, false, "re-start a stopped node" },
6345 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6346 { "unban", control_unban, true, false, "unban a node" },
6347 { "showban", control_showban, true, false, "show ban information"},
6348 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6349 { "recover", control_recover, true, false, "force recovery" },
6350 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6351 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6352 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6353 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6354 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6355 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6356 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6357 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6358 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6360 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6362 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6363 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6364 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6365 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6366 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6367 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6368 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6369 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6370 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6371 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6372 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6373 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6374 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6375 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6376 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6377 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6378 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6379 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6380 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6381 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6382 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6383 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6384 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6385 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6386 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6387 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6388 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6389 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6390 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6391 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6392 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6393 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6394 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6395 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6396 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6397 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6398 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6399 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6400 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6401 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6402 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6403 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6404 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6405 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6406 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6407 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6408 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6409 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6410 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6416 static void usage(void)
6420 "Usage: ctdb [options] <control>\n" \
6422 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6423 " -Y generate machinereadable output\n"
6424 " -v generate verbose output\n"
6425 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6426 printf("Controls:\n");
6427 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6428 printf(" %-15s %-27s %s\n",
6429 ctdb_commands[i].name,
6430 ctdb_commands[i].args?ctdb_commands[i].args:"",
6431 ctdb_commands[i].msg);
6437 static void ctdb_alarm(int sig)
6439 printf("Maximum runtime exceeded - exiting\n");
6446 int main(int argc, const char *argv[])
6448 struct ctdb_context *ctdb;
6449 char *nodestring = NULL;
6450 struct poptOption popt_options[] = {
6453 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6454 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6455 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6456 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6457 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6458 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6459 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6460 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6461 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6462 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6466 const char **extra_argv;
6470 struct event_context *ev;
6471 const char *control;
6475 /* set some defaults */
6476 options.maxruntime = 0;
6477 options.timelimit = 10;
6478 options.pnn = CTDB_CURRENT_NODE;
6480 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6482 while ((opt = poptGetNextOpt(pc)) != -1) {
6485 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6486 poptBadOption(pc, 0), poptStrerror(opt)));
6491 /* setup the remaining options for the main program to use */
6492 extra_argv = poptGetArgs(pc);
6495 while (extra_argv[extra_argc]) extra_argc++;
6498 if (extra_argc < 1) {
6502 if (options.maxruntime == 0) {
6503 const char *ctdb_timeout;
6504 ctdb_timeout = getenv("CTDB_TIMEOUT");
6505 if (ctdb_timeout != NULL) {
6506 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6508 /* default timeout is 120 seconds */
6509 options.maxruntime = 120;
6513 signal(SIGALRM, ctdb_alarm);
6514 alarm(options.maxruntime);
6516 control = extra_argv[0];
6518 /* Default value for CTDB_BASE - don't override */
6519 setenv("CTDB_BASE", ETCDIR "/ctdb", 0);
6521 ev = event_context_init(NULL);
6523 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6527 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6528 if (strcmp(control, ctdb_commands[i].name) == 0) {
6533 if (i == ARRAY_SIZE(ctdb_commands)) {
6534 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6538 if (ctdb_commands[i].without_daemon == true) {
6539 if (nodestring != NULL) {
6540 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6543 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6546 /* initialise ctdb */
6547 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6550 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6554 /* setup the node number(s) to contact */
6555 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6556 &options.nodes, &options.pnn)) {
6560 if (options.pnn == CTDB_CURRENT_NODE) {
6561 options.pnn = options.nodes[0];
6564 if (ctdb_commands[i].auto_all &&
6565 ((options.pnn == CTDB_BROADCAST_ALL) ||
6566 (options.pnn == CTDB_MULTICAST))) {
6570 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6571 options.pnn = options.nodes[j];
6572 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6575 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6580 (void)poptFreeContext(pc);