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,
1186 uint32_t capabilities;
1187 struct ctdb_node_map *ret;
1189 ret = talloc_nodemap(nodemap);
1190 CTDB_NO_MEMORY_NULL(ctdb, ret);
1194 for (i = 0; i < nodemap->num; i++) {
1197 /* Disconnected nodes have no capabilities! */
1198 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1202 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1203 nodemap->nodes[i].pnn,
1206 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1207 nodemap->nodes[i].pnn));
1211 if (!(capabilities & required_capabilities)) {
1215 ret->nodes[ret->num] = nodemap->nodes[i];
1225 static struct ctdb_node_map *
1226 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1227 struct ctdb_node_map *nodemap,
1228 uint32_t flags_mask)
1231 struct ctdb_node_map *ret;
1233 ret = talloc_nodemap(nodemap);
1234 CTDB_NO_MEMORY_NULL(ctdb, ret);
1238 for (i = 0; i < nodemap->num; i++) {
1239 if (nodemap->nodes[i].flags & flags_mask) {
1243 ret->nodes[ret->num] = nodemap->nodes[i];
1251 display the list of nodes belonging to this natgw configuration
1253 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1255 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1257 struct pnn_node *natgw_nodes = NULL;
1258 struct ctdb_node_map *orig_nodemap=NULL;
1259 struct ctdb_node_map *nodemap;
1260 uint32_t mypnn, pnn;
1263 /* When we have some nodes that could be the NATGW, make a
1264 * series of attempts to find the first node that doesn't have
1265 * certain status flags set.
1267 uint32_t exclude_flags[] = {
1268 /* Look for a nice healthy node */
1269 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1270 /* If not found, an UNHEALTHY/BANNED node will do */
1271 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1272 /* If not found, a STOPPED node will do */
1273 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1277 /* read the natgw nodes file into a linked list */
1278 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1279 if (natgw_nodes == NULL) {
1284 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1285 tmp_ctx, &orig_nodemap);
1287 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1288 talloc_free(tmp_ctx);
1292 /* Get a nodemap that includes only the nodes in the NATGW
1294 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1295 if (nodemap == NULL) {
1300 ret = 2; /* matches ENOENT */
1303 /* For each flag mask... */
1304 for (i = 0; exclude_flags[i] != 0; i++) {
1305 /* ... get a nodemap that excludes nodes with with
1306 * masked flags... */
1307 struct ctdb_node_map *t =
1308 filter_nodemap_by_flags(ctdb, nodemap,
1316 /* ... and find the first node with the NATGW
1318 struct ctdb_node_map *n;
1319 n = filter_nodemap_by_capabilities(ctdb, t,
1329 pnn = n->nodes[0].pnn;
1330 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1337 if (options.machinereadable) {
1338 printf(":Node:IP:\n");
1339 printf(":%d:%s:\n", pnn, ip);
1341 printf("%d %s\n", pnn, ip);
1344 /* print the pruned list of nodes belonging to this natgw list */
1345 mypnn = getpnn(ctdb);
1346 if (options.machinereadable) {
1347 control_status_header_machine();
1349 printf("Number of nodes:%d\n", nodemap->num);
1351 for(i=0;i<nodemap->num;i++){
1352 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1355 if (options.machinereadable) {
1356 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1358 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1363 talloc_free(tmp_ctx);
1368 display the status of the scripts for monitoring (or other events)
1370 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1371 enum ctdb_eventscript_call type)
1373 struct ctdb_scripts_wire *script_status;
1376 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1378 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1382 if (script_status == NULL) {
1383 if (!options.machinereadable) {
1384 printf("%s cycle never run\n",
1385 ctdb_eventscript_call_names[type]);
1390 if (!options.machinereadable) {
1392 for (i=0; i<script_status->num_scripts; i++) {
1393 if (script_status->scripts[i].status != -ENOEXEC) {
1397 printf("%d scripts were executed last %s cycle\n",
1399 ctdb_eventscript_call_names[type]);
1401 for (i=0; i<script_status->num_scripts; i++) {
1402 const char *status = NULL;
1404 switch (script_status->scripts[i].status) {
1406 status = "TIMEDOUT";
1409 status = "DISABLED";
1415 if (script_status->scripts[i].status > 0)
1419 if (options.machinereadable) {
1420 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1421 ctdb_eventscript_call_names[type],
1422 script_status->scripts[i].name,
1423 script_status->scripts[i].status,
1425 (long)script_status->scripts[i].start.tv_sec,
1426 (long)script_status->scripts[i].start.tv_usec,
1427 (long)script_status->scripts[i].finished.tv_sec,
1428 (long)script_status->scripts[i].finished.tv_usec,
1429 script_status->scripts[i].output);
1433 printf("%-20s Status:%s ",
1434 script_status->scripts[i].name, status);
1436 /* Some other error, eg from stat. */
1437 printf("%-20s Status:CANNOT RUN (%s)",
1438 script_status->scripts[i].name,
1439 strerror(-script_status->scripts[i].status));
1441 if (script_status->scripts[i].status >= 0) {
1442 printf("Duration:%.3lf ",
1443 timeval_delta(&script_status->scripts[i].finished,
1444 &script_status->scripts[i].start));
1446 if (script_status->scripts[i].status != -ENOEXEC) {
1448 ctime(&script_status->scripts[i].start.tv_sec));
1449 if (script_status->scripts[i].status != 0) {
1450 printf(" OUTPUT:%s\n",
1451 script_status->scripts[i].output);
1461 static int control_scriptstatus(struct ctdb_context *ctdb,
1462 int argc, const char **argv)
1465 enum ctdb_eventscript_call type, min, max;
1469 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1474 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1478 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1479 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1485 if (type == CTDB_EVENT_MAX) {
1486 if (strcmp(arg, "all") == 0) {
1488 max = CTDB_EVENT_MAX;
1490 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1495 if (options.machinereadable) {
1496 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1499 for (type = min; type < max; type++) {
1500 ret = control_one_scriptstatus(ctdb, type);
1510 enable an eventscript
1512 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1520 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1522 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1530 disable an eventscript
1532 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1540 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1542 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1550 display the pnn of the recovery master
1552 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1557 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1559 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1562 printf("%d\n",recmaster);
1568 add a tickle to a public address
1570 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1572 struct ctdb_tcp_connection t;
1576 assert_single_node_only();
1582 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1583 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1586 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1587 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1591 data.dptr = (uint8_t *)&t;
1592 data.dsize = sizeof(t);
1594 /* tell all nodes about this tcp connection */
1595 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1596 0, data, ctdb, NULL, NULL, NULL, NULL);
1598 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1607 delete a tickle from a node
1609 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1611 struct ctdb_tcp_connection t;
1615 assert_single_node_only();
1621 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1622 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1625 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1626 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1630 data.dptr = (uint8_t *)&t;
1631 data.dsize = sizeof(t);
1633 /* tell all nodes about this tcp connection */
1634 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1635 0, data, ctdb, NULL, NULL, NULL, NULL);
1637 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1646 get a list of all tickles for this pnn
1648 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1650 struct ctdb_control_tcp_tickle_list *list;
1651 ctdb_sock_addr addr;
1655 assert_single_node_only();
1662 port = atoi(argv[1]);
1665 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1666 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1670 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1672 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1676 if (options.machinereadable){
1677 printf(":source ip:port:destination ip:port:\n");
1678 for (i=0;i<list->tickles.num;i++) {
1679 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1682 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1683 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1686 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1687 printf("Num tickles:%u\n", list->tickles.num);
1688 for (i=0;i<list->tickles.num;i++) {
1689 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1692 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1693 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));
1703 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1705 struct ctdb_all_public_ips *ips;
1706 struct ctdb_public_ip ip;
1709 uint32_t disable_time;
1711 struct ctdb_node_map *nodemap=NULL;
1712 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1715 data.dptr = (uint8_t*)&disable_time;
1716 data.dsize = sizeof(disable_time);
1717 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1719 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1725 /* read the public ip list from the node */
1726 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1728 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1729 talloc_free(tmp_ctx);
1733 for (i=0;i<ips->num;i++) {
1734 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1739 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1740 pnn, ctdb_addr_to_str(addr)));
1741 talloc_free(tmp_ctx);
1748 data.dptr = (uint8_t *)&ip;
1749 data.dsize = sizeof(ip);
1751 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1753 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1754 talloc_free(tmp_ctx);
1758 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1759 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1766 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1767 talloc_free(tmp_ctx);
1771 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1773 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1774 talloc_free(tmp_ctx);
1778 /* update the recovery daemon so it now knows to expect the new
1779 node assignment for this ip.
1781 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1783 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1787 talloc_free(tmp_ctx);
1793 * scans all other nodes and returns a pnn for another node that can host this
1797 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1799 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1800 struct ctdb_all_public_ips *ips;
1801 struct ctdb_node_map *nodemap=NULL;
1804 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1806 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1807 talloc_free(tmp_ctx);
1811 for(i=0;i<nodemap->num;i++){
1812 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1815 if (nodemap->nodes[i].pnn == options.pnn) {
1819 /* read the public ip list from this node */
1820 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1822 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1826 for (j=0;j<ips->num;j++) {
1827 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1828 talloc_free(tmp_ctx);
1829 return nodemap->nodes[i].pnn;
1835 talloc_free(tmp_ctx);
1839 /* If pnn is -1 then try to find a node to move IP to... */
1840 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1842 bool pnn_specified = (pnn == -1 ? false : true);
1845 while (retries < 5) {
1846 if (!pnn_specified) {
1847 pnn = find_other_host_for_public_ip(ctdb, addr);
1852 ("Trying to move public IP to node %u\n", pnn));
1855 if (move_ip(ctdb, addr, pnn) == 0) {
1868 move/failover an ip address to a specific node
1870 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1873 ctdb_sock_addr addr;
1875 assert_single_node_only();
1882 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1883 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1888 if (sscanf(argv[1], "%u", &pnn) != 1) {
1889 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1893 if (!try_moveip(ctdb, &addr, pnn)) {
1894 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1901 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1905 data.dptr = (uint8_t *)&pnn;
1906 data.dsize = sizeof(uint32_t);
1907 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1909 ("Failed to send message to force node %u to be a rebalancing target\n",
1919 rebalance a node by setting it to allow failback and triggering a
1922 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1924 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1929 assert_single_node_only();
1935 /* Determine the nodes where IPs need to be reloaded */
1936 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1937 options.pnn, true, &nodes, &pnn_mode)) {
1942 for (i = 0; i < talloc_array_length(nodes); i++) {
1943 if (!rebalance_node(ctdb, nodes[i])) {
1949 talloc_free(tmp_ctx);
1953 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1955 struct ctdb_public_ip ip;
1958 uint32_t disable_time;
1960 struct ctdb_node_map *nodemap=NULL;
1961 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1964 data.dptr = (uint8_t*)&disable_time;
1965 data.dsize = sizeof(disable_time);
1966 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1968 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1975 data.dptr = (uint8_t *)&ip;
1976 data.dsize = sizeof(ip);
1978 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1980 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1981 talloc_free(tmp_ctx);
1985 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1986 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1993 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1994 talloc_free(tmp_ctx);
1998 talloc_free(tmp_ctx);
2003 release an ip form all nodes and have it re-assigned by recd
2005 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2007 ctdb_sock_addr addr;
2009 assert_single_node_only();
2016 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2017 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2021 if (rebalance_ip(ctdb, &addr) != 0) {
2022 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2029 static int getips_store_callback(void *param, void *data)
2031 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2032 struct ctdb_all_public_ips *ips = param;
2036 ips->ips[i].pnn = node_ip->pnn;
2037 ips->ips[i].addr = node_ip->addr;
2041 static int getips_count_callback(void *param, void *data)
2043 uint32_t *count = param;
2050 static uint32_t *ip_key(ctdb_sock_addr *ip)
2052 static uint32_t key[IP_KEYLEN];
2054 bzero(key, sizeof(key));
2056 switch (ip->sa.sa_family) {
2058 key[0] = ip->ip.sin_addr.s_addr;
2061 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2069 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2076 static void *add_ip_callback(void *parm, void *data)
2082 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2084 struct ctdb_all_public_ips *tmp_ips;
2085 struct ctdb_node_map *nodemap=NULL;
2086 trbt_tree_t *ip_tree;
2090 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2092 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2096 ip_tree = trbt_create(tmp_ctx, 0);
2098 for(i=0;i<nodemap->num;i++){
2099 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2102 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2106 /* read the public ip list from this node */
2107 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2109 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2113 for (j=0; j<tmp_ips->num;j++) {
2114 struct ctdb_public_ip *node_ip;
2116 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2117 node_ip->pnn = tmp_ips->ips[j].pnn;
2118 node_ip->addr = tmp_ips->ips[j].addr;
2120 trbt_insertarray32_callback(ip_tree,
2121 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2125 talloc_free(tmp_ips);
2130 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2132 len = offsetof(struct ctdb_all_public_ips, ips) +
2133 count*sizeof(struct ctdb_public_ip);
2134 tmp_ips = talloc_zero_size(tmp_ctx, len);
2135 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2143 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2145 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2147 event_add_timed(ctdb->ev, ctdb,
2148 timeval_current_ofs(1, 0),
2149 ctdb_every_second, ctdb);
2152 struct srvid_reply_handler_data {
2156 const char *srvid_str;
2159 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2164 struct srvid_reply_handler_data *d =
2165 (struct srvid_reply_handler_data *)private_data;
2169 if (data.dsize != sizeof(ret)) {
2170 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2174 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2175 ret = *(int32_t *)data.dptr;
2178 ("%s failed with result %d\n", d->srvid_str, ret));
2182 if (!d->wait_for_all) {
2187 /* Wait for all replies */
2189 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2190 if (d->nodes[i] == ret) {
2192 ("%s reply received from node %u\n",
2193 d->srvid_str, ret));
2196 if (d->nodes[i] != -1) {
2197 /* Found a node that hasn't yet replied */
2203 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2204 * or replies from all connected nodes. arg is the data argument to
2205 * pass in the srvid_request structure - pass 0 if this isn't needed.
2207 static int srvid_broadcast(struct ctdb_context *ctdb,
2208 uint64_t srvid, uint32_t *arg,
2209 const char *srvid_str, bool wait_for_all)
2214 uint64_t reply_srvid;
2215 struct srvid_request request;
2216 struct srvid_request_data request_data;
2217 struct srvid_reply_handler_data reply_data;
2220 ZERO_STRUCT(request);
2222 /* Time ticks to enable timeouts to be processed */
2223 event_add_timed(ctdb->ev, ctdb,
2224 timeval_current_ofs(1, 0),
2225 ctdb_every_second, ctdb);
2227 pnn = ctdb_get_pnn(ctdb);
2228 reply_srvid = getpid();
2232 request.srvid = reply_srvid;
2234 data.dptr = (uint8_t *)&request;
2235 data.dsize = sizeof(request);
2237 request_data.pnn = pnn;
2238 request_data.srvid = reply_srvid;
2239 request_data.data = *arg;
2241 data.dptr = (uint8_t *)&request_data;
2242 data.dsize = sizeof(request_data);
2245 /* Register message port for reply from recovery master */
2246 ctdb_client_set_message_handler(ctdb, reply_srvid,
2247 srvid_broadcast_reply_handler,
2250 reply_data.wait_for_all = wait_for_all;
2251 reply_data.nodes = NULL;
2252 reply_data.srvid_str = srvid_str;
2255 reply_data.done = false;
2258 struct ctdb_node_map *nodemap;
2260 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2261 CTDB_CURRENT_NODE, ctdb, &nodemap);
2264 ("Unable to get nodemap from current node, try again\n"));
2269 if (reply_data.nodes != NULL) {
2270 talloc_free(reply_data.nodes);
2272 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2275 talloc_free(nodemap);
2278 /* Send to all connected nodes. Only recmaster replies */
2279 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2282 /* This can only happen if the socket is closed and
2283 * there's no way to recover from that, so don't try
2287 ("Failed to send %s request to connected nodes\n",
2292 tv = timeval_current();
2293 /* This loop terminates the reply is received */
2294 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2295 event_loop_once(ctdb->ev);
2298 if (!reply_data.done) {
2300 ("Still waiting for confirmation of %s\n", srvid_str));
2305 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2307 talloc_free(reply_data.nodes);
2312 static int ipreallocate(struct ctdb_context *ctdb)
2314 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2315 "IP reallocation", false);
2319 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2321 return ipreallocate(ctdb);
2325 add a public ip address to a node
2327 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2330 int len, retries = 0;
2332 ctdb_sock_addr addr;
2333 struct ctdb_control_ip_iface *pub;
2334 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2335 struct ctdb_all_public_ips *ips;
2339 talloc_free(tmp_ctx);
2343 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2344 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2345 talloc_free(tmp_ctx);
2349 /* read the public ip list from the node */
2350 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2352 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2353 talloc_free(tmp_ctx);
2356 for (i=0;i<ips->num;i++) {
2357 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2358 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2365 /* Dont timeout. This command waits for an ip reallocation
2366 which sometimes can take wuite a while if there has
2367 been a recent recovery
2371 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2372 pub = talloc_size(tmp_ctx, len);
2373 CTDB_NO_MEMORY(ctdb, pub);
2377 pub->len = strlen(argv[1])+1;
2378 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2381 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2383 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2387 } while (retries < 5 && ret != 0);
2389 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2390 talloc_free(tmp_ctx);
2394 if (rebalance_node(ctdb, options.pnn) != 0) {
2395 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2399 talloc_free(tmp_ctx);
2404 add a public ip address to a node
2406 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2408 ctdb_sock_addr addr;
2414 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2415 printf("Badly formed ip : %s\n", argv[0]);
2419 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2424 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2426 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2428 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2429 struct ctdb_node_map *nodemap=NULL;
2430 struct ctdb_all_public_ips *ips;
2433 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2435 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2439 /* remove it from the nodes that are not hosting the ip currently */
2440 for(i=0;i<nodemap->num;i++){
2441 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2444 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2446 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2450 for (j=0;j<ips->num;j++) {
2451 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2459 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2463 options.pnn = nodemap->nodes[i].pnn;
2464 control_delip(ctdb, argc, argv);
2468 /* remove it from every node (also the one hosting it) */
2469 for(i=0;i<nodemap->num;i++){
2470 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2473 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2475 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2479 for (j=0;j<ips->num;j++) {
2480 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2488 options.pnn = nodemap->nodes[i].pnn;
2489 control_delip(ctdb, argc, argv);
2492 talloc_free(tmp_ctx);
2497 delete a public ip address from a node
2499 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2502 ctdb_sock_addr addr;
2503 struct ctdb_control_ip_iface pub;
2504 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2505 struct ctdb_all_public_ips *ips;
2508 talloc_free(tmp_ctx);
2512 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2513 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2517 if (options.pnn == CTDB_BROADCAST_ALL) {
2518 return control_delip_all(ctdb, argc, argv, &addr);
2525 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2527 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2528 talloc_free(tmp_ctx);
2532 for (i=0;i<ips->num;i++) {
2533 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2539 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2540 ctdb_addr_to_str(&addr)));
2541 talloc_free(tmp_ctx);
2545 /* This is an optimisation. If this node is hosting the IP
2546 * then try to move it somewhere else without invoking a full
2547 * takeover run. We don't care if this doesn't work!
2549 if (ips->ips[i].pnn == options.pnn) {
2550 (void) try_moveip(ctdb, &addr, -1);
2553 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2555 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2556 talloc_free(tmp_ctx);
2560 talloc_free(tmp_ctx);
2564 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2565 int argc, const char **argv)
2567 struct ctdb_control_killtcp *killtcp;
2568 int max_entries, current, i;
2569 struct timeval timeout;
2570 char line[128], src[128], dst[128];
2573 struct client_async_data *async_data;
2574 struct ctdb_client_control_state *state;
2584 while (!feof(stdin)) {
2585 if (fgets(line, sizeof(line), stdin) == NULL) {
2589 /* Silently skip empty lines */
2590 if (line[0] == '\n') {
2594 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2595 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2597 talloc_free(killtcp);
2601 if (current >= max_entries) {
2602 max_entries += 1024;
2603 killtcp = talloc_realloc(ctdb, killtcp,
2604 struct ctdb_control_killtcp,
2606 CTDB_NO_MEMORY(ctdb, killtcp);
2609 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2610 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2612 talloc_free(killtcp);
2616 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2617 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2619 talloc_free(killtcp);
2626 async_data = talloc_zero(ctdb, struct client_async_data);
2627 if (async_data == NULL) {
2628 talloc_free(killtcp);
2632 for (i = 0; i < current; i++) {
2634 data.dsize = sizeof(struct ctdb_control_killtcp);
2635 data.dptr = (unsigned char *)&killtcp[i];
2637 timeout = TIMELIMIT();
2638 state = ctdb_control_send(ctdb, options.pnn, 0,
2639 CTDB_CONTROL_KILL_TCP, 0, data,
2640 async_data, &timeout, NULL);
2642 if (state == NULL) {
2644 ("Failed to call async killtcp control to node %u\n",
2646 talloc_free(killtcp);
2650 ctdb_client_async_add(async_data, state);
2653 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2654 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2655 talloc_free(killtcp);
2659 talloc_free(killtcp);
2665 kill a tcp connection
2667 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2670 struct ctdb_control_killtcp killtcp;
2672 assert_single_node_only();
2675 return kill_tcp_from_file(ctdb, argc, argv);
2682 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2683 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2687 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2688 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2692 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2694 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2705 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2708 ctdb_sock_addr addr;
2710 assert_single_node_only();
2716 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2717 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2721 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2723 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2731 register a server id
2733 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2736 struct ctdb_server_id server_id;
2742 server_id.pnn = strtoul(argv[0], NULL, 0);
2743 server_id.type = strtoul(argv[1], NULL, 0);
2744 server_id.server_id = strtoul(argv[2], NULL, 0);
2746 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2748 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2751 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2757 unregister a server id
2759 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2762 struct ctdb_server_id server_id;
2768 server_id.pnn = strtoul(argv[0], NULL, 0);
2769 server_id.type = strtoul(argv[1], NULL, 0);
2770 server_id.server_id = strtoul(argv[2], NULL, 0);
2772 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2774 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2781 check if a server id exists
2783 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2787 struct ctdb_server_id server_id;
2793 server_id.pnn = strtoul(argv[0], NULL, 0);
2794 server_id.type = strtoul(argv[1], NULL, 0);
2795 server_id.server_id = strtoul(argv[2], NULL, 0);
2797 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2799 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2804 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2806 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2812 get a list of all server ids that are registered on a node
2814 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2817 struct ctdb_server_id_list *server_ids;
2819 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2821 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2825 for (i=0; i<server_ids->num; i++) {
2826 printf("Server id %d:%d:%d\n",
2827 server_ids->server_ids[i].pnn,
2828 server_ids->server_ids[i].type,
2829 server_ids->server_ids[i].server_id);
2836 check if a server id exists
2838 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2840 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2846 talloc_free(tmp_ctx);
2850 ids = talloc_array(tmp_ctx, uint64_t, argc);
2851 result = talloc_array(tmp_ctx, uint8_t, argc);
2853 for (i = 0; i < argc; i++) {
2854 ids[i] = strtoull(argv[i], NULL, 0);
2857 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2858 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2860 talloc_free(tmp_ctx);
2864 for (i=0; i < argc; i++) {
2865 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2866 result[i] ? "exists" : "does not exist");
2869 talloc_free(tmp_ctx);
2874 send a tcp tickle ack
2876 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2879 ctdb_sock_addr src, dst;
2885 if (!parse_ip_port(argv[0], &src)) {
2886 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2890 if (!parse_ip_port(argv[1], &dst)) {
2891 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2895 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2899 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2906 display public ip status
2908 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2911 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2912 struct ctdb_all_public_ips *ips;
2914 if (options.pnn == CTDB_BROADCAST_ALL) {
2915 /* read the list of public ips from all nodes */
2916 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2918 /* read the public ip list from this node */
2919 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2922 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2923 talloc_free(tmp_ctx);
2927 if (options.machinereadable){
2928 printf(":Public IP:Node:");
2929 if (options.verbose){
2930 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2934 if (options.pnn == CTDB_BROADCAST_ALL) {
2935 printf("Public IPs on ALL nodes\n");
2937 printf("Public IPs on node %u\n", options.pnn);
2941 for (i=1;i<=ips->num;i++) {
2942 struct ctdb_control_public_ip_info *info = NULL;
2944 char *aciface = NULL;
2945 char *avifaces = NULL;
2946 char *cifaces = NULL;
2948 if (options.pnn == CTDB_BROADCAST_ALL) {
2949 pnn = ips->ips[ips->num-i].pnn;
2955 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2956 &ips->ips[ips->num-i].addr, &info);
2963 for (j=0; j < info->num; j++) {
2964 if (cifaces == NULL) {
2965 cifaces = talloc_strdup(info,
2966 info->ifaces[j].name);
2968 cifaces = talloc_asprintf_append(cifaces,
2970 info->ifaces[j].name);
2973 if (info->active_idx == j) {
2974 aciface = info->ifaces[j].name;
2977 if (info->ifaces[j].link_state == 0) {
2981 if (avifaces == NULL) {
2982 avifaces = talloc_strdup(info, info->ifaces[j].name);
2984 avifaces = talloc_asprintf_append(avifaces,
2986 info->ifaces[j].name);
2991 if (options.machinereadable){
2993 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2994 ips->ips[ips->num-i].pnn);
2995 if (options.verbose){
2998 avifaces?avifaces:"",
2999 cifaces?cifaces:"");
3003 if (options.verbose) {
3004 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3005 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3006 ips->ips[ips->num-i].pnn,
3008 avifaces?avifaces:"",
3009 cifaces?cifaces:"");
3012 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3013 ips->ips[ips->num-i].pnn);
3019 talloc_free(tmp_ctx);
3026 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3029 ctdb_sock_addr addr;
3030 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3031 struct ctdb_control_public_ip_info *info;
3034 talloc_free(tmp_ctx);
3038 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3039 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3043 /* read the public ip info from this node */
3044 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3045 tmp_ctx, &addr, &info);
3047 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3048 argv[0], options.pnn));
3049 talloc_free(tmp_ctx);
3053 printf("Public IP[%s] info on node %u\n",
3054 ctdb_addr_to_str(&info->ip.addr),
3057 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3058 ctdb_addr_to_str(&info->ip.addr),
3059 info->ip.pnn, info->num);
3061 for (i=0; i<info->num; i++) {
3062 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3064 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3065 i+1, info->ifaces[i].name,
3066 info->ifaces[i].link_state?"up":"down",
3067 (unsigned int)info->ifaces[i].references,
3068 (i==info->active_idx)?" (active)":"");
3071 talloc_free(tmp_ctx);
3076 display interfaces status
3078 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3080 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3082 struct ctdb_control_get_ifaces *ifaces;
3085 /* read the public ip list from this node */
3086 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3088 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3090 talloc_free(tmp_ctx);
3094 if (options.machinereadable){
3095 printf(":Name:LinkStatus:References:\n");
3097 printf("Interfaces on node %u\n", options.pnn);
3100 for (i=0; i<ifaces->num; i++) {
3101 if (options.machinereadable){
3102 printf(":%s:%s:%u\n",
3103 ifaces->ifaces[i].name,
3104 ifaces->ifaces[i].link_state?"1":"0",
3105 (unsigned int)ifaces->ifaces[i].references);
3107 printf("name:%s link:%s references:%u\n",
3108 ifaces->ifaces[i].name,
3109 ifaces->ifaces[i].link_state?"up":"down",
3110 (unsigned int)ifaces->ifaces[i].references);
3114 talloc_free(tmp_ctx);
3120 set link status of an interface
3122 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3125 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3126 struct ctdb_control_iface_info info;
3134 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3135 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3137 talloc_free(tmp_ctx);
3140 strcpy(info.name, argv[0]);
3142 if (strcmp(argv[1], "up") == 0) {
3143 info.link_state = 1;
3144 } else if (strcmp(argv[1], "down") == 0) {
3145 info.link_state = 0;
3147 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3149 talloc_free(tmp_ctx);
3153 /* read the public ip list from this node */
3154 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3157 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3158 argv[0], options.pnn));
3159 talloc_free(tmp_ctx);
3163 talloc_free(tmp_ctx);
3168 display pid of a ctdb daemon
3170 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3175 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3177 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3180 printf("Pid:%d\n", pid);
3185 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3187 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3189 update_flags_handler_t handler,
3194 struct ctdb_node_map *nodemap = NULL;
3198 /* Check if the node is already in the desired state */
3199 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3201 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3204 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3205 if (set_flag == flag_is_set) {
3206 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3207 (set_flag ? "already" : "not"), desc));
3212 if (!handler(ctdb, data)) {
3213 DEBUG(DEBUG_WARNING,
3214 ("Failed to send control to set state %s on node %u, try again\n",
3215 desc, options.pnn));
3220 /* Read the nodemap and verify the change took effect.
3221 * Even if the above control/hanlder timed out then it
3222 * could still have worked!
3224 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3227 DEBUG(DEBUG_WARNING,
3228 ("Unable to get nodemap from local node, try again\n"));
3230 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3231 } while (nodemap == NULL || (set_flag != flag_is_set));
3233 return ipreallocate(ctdb);
3236 /* Administratively disable a node */
3237 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3241 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3242 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3246 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3248 return update_flags_and_ipreallocate(ctdb, NULL,
3249 update_flags_disabled,
3250 NODE_FLAGS_PERMANENTLY_DISABLED,
3252 true /* set_flag*/);
3255 /* Administratively re-enable a node */
3256 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3260 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3261 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3265 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3267 return update_flags_and_ipreallocate(ctdb, NULL,
3268 update_flags_not_disabled,
3269 NODE_FLAGS_PERMANENTLY_DISABLED,
3271 false /* set_flag*/);
3275 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3279 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3284 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3286 return update_flags_and_ipreallocate(ctdb, NULL,
3287 update_flags_stopped,
3290 true /* set_flag*/);
3293 /* Continue a stopped node */
3294 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3298 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3303 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3305 return update_flags_and_ipreallocate(ctdb, NULL,
3306 update_flags_not_stopped,
3309 false /* set_flag */);
3312 static uint32_t get_generation(struct ctdb_context *ctdb)
3314 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3315 struct ctdb_vnn_map *vnnmap=NULL;
3317 uint32_t generation;
3319 /* wait until the recmaster is not in recovery mode */
3321 uint32_t recmode, recmaster;
3323 if (vnnmap != NULL) {
3324 talloc_free(vnnmap);
3328 /* get the recmaster */
3329 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3331 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3332 talloc_free(tmp_ctx);
3336 /* get recovery mode */
3337 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3339 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3340 talloc_free(tmp_ctx);
3344 /* get the current generation number */
3345 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3347 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3348 talloc_free(tmp_ctx);
3352 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3353 generation = vnnmap->generation;
3354 talloc_free(tmp_ctx);
3362 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3364 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3367 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3372 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3374 struct ctdb_ban_time bantime;
3380 bantime.pnn = options.pnn;
3381 bantime.time = strtoul(argv[0], NULL, 0);
3383 if (bantime.time == 0) {
3384 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3388 return update_flags_and_ipreallocate(ctdb, &bantime,
3389 update_state_banned,
3392 true /* set_flag*/);
3397 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3399 struct ctdb_ban_time bantime;
3401 bantime.pnn = options.pnn;
3404 return update_flags_and_ipreallocate(ctdb, &bantime,
3405 update_state_banned,
3408 false /* set_flag*/);
3412 show ban information for a node
3414 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3417 struct ctdb_node_map *nodemap=NULL;
3418 struct ctdb_ban_time *bantime;
3420 /* verify the node exists */
3421 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3423 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3427 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3429 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3433 if (bantime->time == 0) {
3434 printf("Node %u is not banned\n", bantime->pnn);
3436 printf("Node %u is banned, %d seconds remaining\n",
3437 bantime->pnn, bantime->time);
3446 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3450 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3452 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3462 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3465 uint32_t generation, next_generation;
3468 /* "force" option ignores freeze failure and forces recovery */
3469 force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3471 /* record the current generation number */
3472 generation = get_generation(ctdb);
3474 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3477 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3480 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3483 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3485 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3489 /* wait until we are in a new generation */
3491 next_generation = get_generation(ctdb);
3492 if (next_generation != generation) {
3503 display monitoring mode of a remote node
3505 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3510 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3512 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3515 if (!options.machinereadable){
3516 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3519 printf(":%d:\n",monmode);
3526 display capabilities of a remote node
3528 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3530 uint32_t capabilities;
3533 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3535 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3539 if (!options.machinereadable){
3540 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3541 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3542 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3543 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3545 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3546 printf(":%d:%d:%d:%d:\n",
3547 !!(capabilities&CTDB_CAP_RECMASTER),
3548 !!(capabilities&CTDB_CAP_LMASTER),
3549 !!(capabilities&CTDB_CAP_LVS),
3550 !!(capabilities&CTDB_CAP_NATGW));
3556 display lvs configuration
3559 static uint32_t lvs_exclude_flags[] = {
3560 /* Look for a nice healthy node */
3561 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3562 /* If not found, an UNHEALTHY node will do */
3563 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3567 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3569 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3570 struct ctdb_node_map *orig_nodemap=NULL;
3571 struct ctdb_node_map *nodemap;
3574 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3575 tmp_ctx, &orig_nodemap);
3577 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3578 talloc_free(tmp_ctx);
3582 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3583 CTDB_CAP_LVS, false);
3584 if (nodemap == NULL) {
3592 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3593 struct ctdb_node_map *t =
3594 filter_nodemap_by_flags(ctdb, nodemap,
3595 lvs_exclude_flags[i]);
3602 /* At least 1 node without excluded flags */
3604 for (j = 0; j < t->num; j++) {
3605 printf("%d:%s\n", t->nodes[j].pnn,
3606 ctdb_addr_to_str(&t->nodes[j].addr));
3613 talloc_free(tmp_ctx);
3618 display who is the lvs master
3620 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3622 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3623 struct ctdb_node_map *nodemap=NULL;
3626 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3629 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3630 talloc_free(tmp_ctx);
3634 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3635 struct ctdb_node_map *t =
3636 filter_nodemap_by_flags(ctdb, nodemap,
3637 lvs_exclude_flags[i]);
3644 struct ctdb_node_map *n;
3645 n = filter_nodemap_by_capabilities(ctdb,
3656 printf(options.machinereadable ?
3657 "%d\n" : "Node %d is LVS master\n",
3665 printf("There is no LVS master\n");
3668 talloc_free(tmp_ctx);
3673 disable monitoring on a node
3675 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3680 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3682 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3685 printf("Monitoring mode:%s\n","DISABLED");
3691 enable monitoring on a node
3693 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3698 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3700 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3703 printf("Monitoring mode:%s\n","ACTIVE");
3709 display remote list of keys/data for a db
3711 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3713 const char *db_name;
3714 struct ctdb_db_context *ctdb_db;
3716 struct ctdb_dump_db_context c;
3723 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3727 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3728 if (ctdb_db == NULL) {
3729 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3733 if (options.printlmaster) {
3734 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3735 ctdb, &ctdb->vnn_map);
3737 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3745 c.printemptyrecords = (bool)options.printemptyrecords;
3746 c.printdatasize = (bool)options.printdatasize;
3747 c.printlmaster = (bool)options.printlmaster;
3748 c.printhash = (bool)options.printhash;
3749 c.printrecordflags = (bool)options.printrecordflags;
3751 /* traverse and dump the cluster tdb */
3752 ret = ctdb_dump_db(ctdb_db, &c);
3754 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3755 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3756 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3760 talloc_free(ctdb_db);
3762 printf("Dumped %d records\n", ret);
3766 struct cattdb_data {
3767 struct ctdb_context *ctdb;
3771 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3773 struct cattdb_data *d = private_data;
3774 struct ctdb_dump_db_context c;
3780 c.printemptyrecords = (bool)options.printemptyrecords;
3781 c.printdatasize = (bool)options.printdatasize;
3782 c.printlmaster = false;
3783 c.printhash = (bool)options.printhash;
3784 c.printrecordflags = true;
3786 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3790 cat the local tdb database using same format as catdb
3792 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3794 const char *db_name;
3795 struct ctdb_db_context *ctdb_db;
3796 struct cattdb_data d;
3803 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3807 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3808 if (ctdb_db == NULL) {
3809 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3813 /* traverse the local tdb */
3816 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3817 printf("Failed to cattdb data\n");
3820 talloc_free(ctdb_db);
3822 printf("Dumped %d records\n", d.count);
3827 display the content of a database key
3829 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3831 const char *db_name;
3832 struct ctdb_db_context *ctdb_db;
3833 struct ctdb_record_handle *h;
3834 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3842 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3846 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3847 if (ctdb_db == NULL) {
3848 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3852 key.dptr = discard_const(argv[1]);
3853 key.dsize = strlen((char *)key.dptr);
3855 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3857 printf("Failed to fetch record '%s' on node %d\n",
3858 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3859 talloc_free(tmp_ctx);
3863 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3865 talloc_free(tmp_ctx);
3866 talloc_free(ctdb_db);
3871 display the content of a database key
3873 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3875 const char *db_name;
3876 struct ctdb_db_context *ctdb_db;
3877 struct ctdb_record_handle *h;
3878 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3886 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3890 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3891 if (ctdb_db == NULL) {
3892 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3896 key.dptr = discard_const(argv[1]);
3897 key.dsize = strlen((char *)key.dptr);
3899 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3901 printf("Failed to fetch record '%s' on node %d\n",
3902 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3903 talloc_free(tmp_ctx);
3907 data.dptr = discard_const(argv[2]);
3908 data.dsize = strlen((char *)data.dptr);
3910 if (ctdb_record_store(h, data) != 0) {
3911 printf("Failed to store record\n");
3915 talloc_free(tmp_ctx);
3916 talloc_free(ctdb_db);
3921 fetch a record from a persistent database
3923 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3925 const char *db_name;
3926 struct ctdb_db_context *ctdb_db;
3927 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3928 struct ctdb_transaction_handle *h;
3935 talloc_free(tmp_ctx);
3939 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3940 talloc_free(tmp_ctx);
3944 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3946 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3947 talloc_free(tmp_ctx);
3951 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3952 if (ctdb_db == NULL) {
3953 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3954 talloc_free(tmp_ctx);
3958 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3960 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3961 talloc_free(tmp_ctx);
3965 key.dptr = discard_const(argv[1]);
3966 key.dsize = strlen(argv[1]);
3967 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3969 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3970 talloc_free(tmp_ctx);
3974 if (data.dsize == 0 || data.dptr == NULL) {
3975 DEBUG(DEBUG_ERR,("Record is empty\n"));
3976 talloc_free(tmp_ctx);
3981 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3983 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3984 talloc_free(tmp_ctx);
3987 write(fd, data.dptr, data.dsize);
3990 write(1, data.dptr, data.dsize);
3993 /* abort the transaction */
3997 talloc_free(tmp_ctx);
4002 fetch a record from a tdb-file
4004 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4006 const char *tdb_file;
4009 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4018 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4020 printf("Failed to open TDB file %s\n", tdb_file);
4024 if (!strncmp(argv[1], "0x", 2)) {
4025 key = hextodata(tmp_ctx, argv[1] + 2);
4026 if (key.dsize == 0) {
4027 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4031 key.dptr = discard_const(argv[1]);
4032 key.dsize = strlen(argv[1]);
4035 data = tdb_fetch(tdb, key);
4036 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4037 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4045 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4047 printf("Failed to open output file %s\n", argv[2]);
4050 if (options.verbose){
4051 write(fd, data.dptr, data.dsize);
4053 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4057 if (options.verbose){
4058 write(1, data.dptr, data.dsize);
4060 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4064 talloc_free(tmp_ctx);
4069 store a record and header to a tdb-file
4071 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4073 const char *tdb_file;
4075 TDB_DATA key, value, data;
4076 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4077 struct ctdb_ltdb_header header;
4085 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4087 printf("Failed to open TDB file %s\n", tdb_file);
4091 if (!strncmp(argv[1], "0x", 2)) {
4092 key = hextodata(tmp_ctx, argv[1] + 2);
4093 if (key.dsize == 0) {
4094 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4098 key.dptr = discard_const(argv[1]);
4099 key.dsize = strlen(argv[1]);
4102 if (!strncmp(argv[2], "0x", 2)) {
4103 value = hextodata(tmp_ctx, argv[2] + 2);
4104 if (value.dsize == 0) {
4105 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4109 value.dptr = discard_const(argv[2]);
4110 value.dsize = strlen(argv[2]);
4113 ZERO_STRUCT(header);
4115 header.rsn = atoll(argv[3]);
4118 header.dmaster = atoi(argv[4]);
4121 header.flags = atoi(argv[5]);
4124 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4125 data.dptr = talloc_size(tmp_ctx, data.dsize);
4126 if (data.dptr == NULL) {
4127 printf("Failed to allocate header+value\n");
4131 *(struct ctdb_ltdb_header *)data.dptr = header;
4132 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4134 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4135 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4142 talloc_free(tmp_ctx);
4147 write a record to a persistent database
4149 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4151 const char *db_name;
4152 struct ctdb_db_context *ctdb_db;
4153 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4154 struct ctdb_transaction_handle *h;
4160 talloc_free(tmp_ctx);
4164 fd = open(argv[2], O_RDONLY);
4166 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4167 talloc_free(tmp_ctx);
4171 ret = fstat(fd, &st);
4173 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4175 talloc_free(tmp_ctx);
4179 if (!S_ISREG(st.st_mode)) {
4180 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4182 talloc_free(tmp_ctx);
4186 data.dsize = st.st_size;
4187 if (data.dsize == 0) {
4190 data.dptr = talloc_size(tmp_ctx, data.dsize);
4191 if (data.dptr == NULL) {
4192 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4194 talloc_free(tmp_ctx);
4197 ret = read(fd, data.dptr, data.dsize);
4198 if (ret != data.dsize) {
4199 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4201 talloc_free(tmp_ctx);
4210 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4211 if (ctdb_db == NULL) {
4212 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4213 talloc_free(tmp_ctx);
4217 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4219 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4220 talloc_free(tmp_ctx);
4224 key.dptr = discard_const(argv[1]);
4225 key.dsize = strlen(argv[1]);
4226 ret = ctdb_transaction_store(h, key, data);
4228 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4229 talloc_free(tmp_ctx);
4233 ret = ctdb_transaction_commit(h);
4235 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4236 talloc_free(tmp_ctx);
4241 talloc_free(tmp_ctx);
4246 * delete a record from a persistent database
4248 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4250 const char *db_name;
4251 struct ctdb_db_context *ctdb_db;
4252 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4253 struct ctdb_transaction_handle *h;
4260 talloc_free(tmp_ctx);
4264 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4265 talloc_free(tmp_ctx);
4269 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4271 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4272 talloc_free(tmp_ctx);
4276 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4277 if (ctdb_db == NULL) {
4278 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4279 talloc_free(tmp_ctx);
4283 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4285 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4286 talloc_free(tmp_ctx);
4290 key.dptr = discard_const(argv[1]);
4291 key.dsize = strlen(argv[1]);
4292 ret = ctdb_transaction_store(h, key, tdb_null);
4294 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4295 talloc_free(tmp_ctx);
4299 ret = ctdb_transaction_commit(h);
4301 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4302 talloc_free(tmp_ctx);
4306 talloc_free(tmp_ctx);
4310 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4315 const char *ret; /* Next byte after successfully parsed value */
4317 /* Error, unless someone says otherwise */
4319 /* Indicates no value to parse */
4322 /* Skip whitespace */
4323 n = strspn(s, " \t");
4327 /* Quoted ASCII string - no wide characters! */
4329 n = strcspn(t, "\"");
4333 data->dptr = talloc_memdup(mem_ctx, t, n);
4334 CTDB_NOMEM_ABORT(data->dptr);
4338 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4341 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4347 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4348 TDB_DATA *key, TDB_DATA *value)
4350 char line [1024]; /* FIXME: make this more flexible? */
4354 ptr = fgets(line, sizeof(line), file);
4361 t = ptrans_parse_string(mem_ctx, line, key);
4362 if (t == NULL || key->dptr == NULL) {
4363 /* Line Ignored but not EOF */
4368 t = ptrans_parse_string(mem_ctx, t, value);
4370 /* Line Ignored but not EOF */
4371 talloc_free(key->dptr);
4380 * Update a persistent database as per file/stdin
4382 static int control_ptrans(struct ctdb_context *ctdb,
4383 int argc, const char **argv)
4385 const char *db_name;
4386 struct ctdb_db_context *ctdb_db;
4387 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4388 struct ctdb_transaction_handle *h;
4389 TDB_DATA key, value;
4394 talloc_free(tmp_ctx);
4400 file = fopen(argv[1], "r");
4402 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4403 talloc_free(tmp_ctx);
4410 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4411 if (ctdb_db == NULL) {
4412 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4416 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4418 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4422 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4423 if (key.dsize != 0) {
4424 ret = ctdb_transaction_store(h, key, value);
4425 /* Minimise memory use */
4426 talloc_free(key.dptr);
4427 if (value.dptr != NULL) {
4428 talloc_free(value.dptr);
4431 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4432 ctdb_transaction_cancel(h);
4438 ret = ctdb_transaction_commit(h);
4440 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4444 if (file != stdin) {
4447 talloc_free(tmp_ctx);
4451 if (file != stdin) {
4455 talloc_free(tmp_ctx);
4460 check if a service is bound to a port or not
4462 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4467 struct sockaddr_in sin;
4470 printf("Use: ctdb chktcport <port>\n");
4474 port = atoi(argv[0]);
4476 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4478 printf("Failed to open local socket\n");
4482 v = fcntl(s, F_GETFL, 0);
4483 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4484 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4487 bzero(&sin, sizeof(sin));
4488 sin.sin_family = PF_INET;
4489 sin.sin_port = htons(port);
4490 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4493 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4502 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4503 TDB_DATA data, void *private_data)
4505 DEBUG(DEBUG_ERR,("Log data received\n"));
4506 if (data.dsize > 0) {
4507 printf("%s", data.dptr);
4514 display a list of log messages from the in memory ringbuffer
4516 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4520 struct ctdb_get_log_addr log_addr;
4524 /* Process options */
4526 log_addr.pnn = ctdb_get_pnn(ctdb);
4527 log_addr.level = DEBUG_NOTICE;
4528 for (i = 0; i < argc; i++) {
4529 if (strcmp(argv[i], "recoverd") == 0) {
4530 main_daemon = false;
4532 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4533 log_addr.level = get_debug_by_desc(argv[i]);
4535 log_addr.level = strtol(argv[i], NULL, 0);
4540 /* Our message port is our PID */
4541 log_addr.srvid = getpid();
4543 data.dptr = (unsigned char *)&log_addr;
4544 data.dsize = sizeof(log_addr);
4546 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4548 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4551 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4556 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4558 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4559 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4560 if (ret != 0 || res != 0) {
4561 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4562 talloc_free(tmp_ctx);
4565 talloc_free(tmp_ctx);
4567 ret = ctdb_client_send_message(ctdb, options.pnn,
4568 CTDB_SRVID_GETLOG, data);
4570 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4575 tv = timeval_current();
4576 /* this loop will terminate when we have received the reply */
4577 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4578 event_loop_once(ctdb->ev);
4581 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4587 clear the in memory log area
4589 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4593 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4594 /* "recoverd" not given - get logs from main daemon */
4597 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4599 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4600 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4601 if (ret != 0 || res != 0) {
4602 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4603 talloc_free(tmp_ctx);
4607 talloc_free(tmp_ctx);
4609 TDB_DATA data; /* unused in recoverd... */
4612 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4614 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4622 /* Reload public IPs on a specified nodes */
4623 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4625 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4631 assert_single_node_only();
4637 /* Determine the nodes where IPs need to be reloaded */
4638 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4639 options.pnn, true, &nodes, &pnn_mode)) {
4645 /* Disable takeover runs on all connected nodes. A reply
4646 * indicating success is needed from each node so all nodes
4647 * will need to be active. This will retry until maxruntime
4648 * is exceeded, hence no error handling.
4650 * A check could be added to not allow reloading of IPs when
4651 * there are disconnected nodes. However, this should
4652 * probably be left up to the administrator.
4654 timeout = LONGTIMEOUT;
4655 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4656 "Disable takeover runs", true);
4658 /* Now tell all the desired nodes to reload their public IPs.
4659 * Keep trying this until it succeeds. This assumes all
4660 * failures are transient, which might not be true...
4662 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4663 nodes, 0, LONGTIMELIMIT(),
4665 NULL, NULL, NULL) != 0) {
4667 ("Unable to reload IPs on some nodes, try again.\n"));
4671 /* It isn't strictly necessary to wait until takeover runs are
4672 * re-enabled but doing so can't hurt.
4675 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4676 "Enable takeover runs", true);
4682 talloc_free(tmp_ctx);
4687 display a list of the databases on a remote ctdb
4689 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4692 struct ctdb_dbid_map *dbmap=NULL;
4694 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4696 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4700 if(options.machinereadable){
4701 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4702 for(i=0;i<dbmap->num;i++){
4710 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4711 dbmap->dbs[i].dbid, ctdb, &path);
4712 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4713 dbmap->dbs[i].dbid, ctdb, &name);
4714 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4715 dbmap->dbs[i].dbid, ctdb, &health);
4716 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4717 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4718 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4719 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4720 dbmap->dbs[i].dbid, name, path,
4721 !!(persistent), !!(sticky),
4722 !!(health), !!(readonly));
4727 printf("Number of databases:%d\n", dbmap->num);
4728 for(i=0;i<dbmap->num;i++){
4736 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4737 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4738 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4739 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4740 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4741 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4742 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4743 dbmap->dbs[i].dbid, name, path,
4744 persistent?" PERSISTENT":"",
4745 sticky?" STICKY":"",
4746 readonly?" READONLY":"",
4747 health?" UNHEALTHY":"");
4754 display the status of a database on a remote ctdb
4756 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4758 const char *db_name;
4768 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4772 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4773 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4774 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4775 db_id, db_name, path,
4776 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4777 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4778 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4779 (health ? health : "OK"));
4785 check if the local node is recmaster or not
4786 it will return 1 if this node is the recmaster and 0 if it is not
4787 or if the local ctdb daemon could not be contacted
4789 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4791 uint32_t mypnn, recmaster;
4794 assert_single_node_only();
4796 mypnn = getpnn(ctdb);
4798 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4800 printf("Failed to get the recmaster\n");
4804 if (recmaster != mypnn) {
4805 printf("this node is not the recmaster\n");
4809 printf("this node is the recmaster\n");
4816 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4819 struct timeval tv = timeval_current();
4820 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4822 printf("Unable to get ping response from node %u\n", options.pnn);
4825 printf("response from %u time=%.6f sec (%d clients)\n",
4826 options.pnn, timeval_elapsed(&tv), ret);
4833 get a node's runstate
4835 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4838 enum ctdb_runstate runstate;
4840 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4842 printf("Unable to get runstate response from node %u\n",
4847 enum ctdb_runstate t;
4849 for (i=0; i<argc; i++) {
4851 t = runstate_from_string(argv[i]);
4852 if (t == CTDB_RUNSTATE_UNKNOWN) {
4853 printf("Invalid run state (%s)\n", argv[i]);
4857 if (t == runstate) {
4864 printf("CTDB not in required run state (got %s)\n",
4865 runstate_to_string((enum ctdb_runstate)runstate));
4870 printf("%s\n", runstate_to_string(runstate));
4878 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4889 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4891 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4895 printf("%-23s = %u\n", name, value);
4902 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4913 value = strtoul(argv[1], NULL, 0);
4915 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4917 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4926 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4932 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4934 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4938 for (i=0;i<count;i++) {
4939 control_getvar(ctdb, 1, &list[i]);
4948 display debug level on a node
4950 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4955 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4957 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4960 if (options.machinereadable){
4961 printf(":Name:Level:\n");
4962 printf(":%s:%d:\n",get_debug_by_level(level),level);
4964 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4971 display reclock file of a node
4973 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4976 const char *reclock;
4978 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4980 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4983 if (options.machinereadable){
4984 if (reclock != NULL) {
4985 printf("%s", reclock);
4988 if (reclock == NULL) {
4989 printf("No reclock file used.\n");
4991 printf("Reclock file:%s\n", reclock);
4999 set the reclock file of a node
5001 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
5004 const char *reclock;
5008 } else if (argc == 1) {
5014 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
5016 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
5023 set the natgw state on/off
5025 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
5028 uint32_t natgwstate;
5034 if (!strcmp(argv[0], "on")) {
5036 } else if (!strcmp(argv[0], "off")) {
5042 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
5044 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
5052 set the lmaster role on/off
5054 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5057 uint32_t lmasterrole;
5063 if (!strcmp(argv[0], "on")) {
5065 } else if (!strcmp(argv[0], "off")) {
5071 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
5073 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
5081 set the recmaster role on/off
5083 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5086 uint32_t recmasterrole;
5092 if (!strcmp(argv[0], "on")) {
5094 } else if (!strcmp(argv[0], "off")) {
5100 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5102 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5110 set debug level on a node or all nodes
5112 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5118 printf("You must specify the debug level. Valid levels are:\n");
5119 for (i=0; debug_levels[i].description != NULL; i++) {
5120 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5126 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
5127 level = get_debug_by_desc(argv[0]);
5129 level = strtol(argv[0], NULL, 0);
5132 for (i=0; debug_levels[i].description != NULL; i++) {
5133 if (level == debug_levels[i].level) {
5137 if (debug_levels[i].description == NULL) {
5138 printf("Invalid debug level, must be one of\n");
5139 for (i=0; debug_levels[i].description != NULL; i++) {
5140 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5145 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5147 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5156 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5162 priority = strtol(argv[0], NULL, 0);
5166 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5168 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5170 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5177 attach to a database
5179 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5181 const char *db_name;
5182 struct ctdb_db_context *ctdb_db;
5183 bool persistent = false;
5193 if (strcmp(argv[1], "persistent") != 0) {
5199 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5200 if (ctdb_db == NULL) {
5201 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5209 * detach from a database
5211 static int control_detach(struct ctdb_context *ctdb, int argc,
5216 int ret, i, status = 0;
5217 struct ctdb_node_map *nodemap = NULL;
5218 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5225 assert_single_node_only();
5227 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5230 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5231 "when recovery is active\n"));
5232 talloc_free(tmp_ctx);
5236 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5239 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5241 talloc_free(tmp_ctx);
5245 for (i=0; i<nodemap->num; i++) {
5248 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5252 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5256 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5257 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5258 "inactive (stopped or banned) node "
5259 "%u\n", nodemap->nodes[i].pnn));
5260 talloc_free(tmp_ctx);
5264 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5265 nodemap->nodes[i].pnn,
5266 "AllowClientDBAttach",
5269 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5270 "AllowClientDBAttach from node %u\n",
5271 nodemap->nodes[i].pnn));
5272 talloc_free(tmp_ctx);
5277 DEBUG(DEBUG_ERR, ("Database access is still active on "
5278 "node %u. Set AllowClientDBAttach=0 "
5280 nodemap->nodes[i].pnn));
5281 talloc_free(tmp_ctx);
5286 talloc_free(tmp_ctx);
5288 for (i=0; i<argc; i++) {
5289 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5293 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5294 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5295 "cannot be detached\n", argv[i]));
5300 ret = ctdb_detach(ctdb, db_id);
5302 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5314 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5316 struct ctdb_db_priority db_prio;
5323 db_prio.db_id = strtoul(argv[0], NULL, 0);
5324 db_prio.priority = strtoul(argv[1], NULL, 0);
5326 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5328 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5338 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5340 uint32_t db_id, priority;
5347 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5351 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5353 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5357 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5363 set the sticky records capability for a database
5365 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5367 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5375 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5379 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5381 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5382 talloc_free(tmp_ctx);
5386 talloc_free(tmp_ctx);
5391 set the readonly capability for a database
5393 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5395 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5403 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5407 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5409 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5410 talloc_free(tmp_ctx);
5414 talloc_free(tmp_ctx);
5421 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5431 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5435 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5437 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5441 printf("Sequence number:%lld\n", (long long)seqnum);
5447 run an eventscript on a node
5449 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5455 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5458 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5462 data.dptr = (unsigned char *)discard_const(argv[0]);
5463 data.dsize = strlen((char *)data.dptr) + 1;
5465 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5467 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5468 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5469 if (ret != 0 || res != 0) {
5470 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5471 talloc_free(tmp_ctx);
5474 talloc_free(tmp_ctx);
5478 #define DB_VERSION 1
5479 #define MAX_DB_NAME 64
5480 struct db_file_header {
5481 unsigned long version;
5483 unsigned long persistent;
5485 const char name[MAX_DB_NAME];
5488 struct backup_data {
5489 struct ctdb_marshall_buffer *records;
5492 bool traverse_error;
5495 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5497 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5498 struct ctdb_rec_data *rec;
5500 /* add the record */
5501 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5503 bd->traverse_error = true;
5504 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5507 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5508 if (bd->records == NULL) {
5509 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5510 bd->traverse_error = true;
5513 bd->records->count++;
5514 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5515 bd->len += rec->length;
5523 * backup a database to a file
5525 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5527 const char *db_name;
5529 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5530 struct db_file_header dbhdr;
5531 struct ctdb_db_context *ctdb_db;
5532 struct backup_data *bd;
5535 const char *reason = NULL;
5539 assert_single_node_only();
5542 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5546 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5550 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5551 db_id, tmp_ctx, &reason);
5553 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5555 talloc_free(tmp_ctx);
5559 uint32_t allow_unhealthy = 0;
5561 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5562 "AllowUnhealthyDBRead",
5565 if (allow_unhealthy != 1) {
5566 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5569 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5571 talloc_free(tmp_ctx);
5575 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5577 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5578 "tunnable AllowUnhealthyDBRead = %u\n",
5582 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5583 if (ctdb_db == NULL) {
5584 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5585 talloc_free(tmp_ctx);
5590 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5592 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5593 talloc_free(tmp_ctx);
5598 bd = talloc_zero(tmp_ctx, struct backup_data);
5600 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5601 talloc_free(tmp_ctx);
5605 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5606 if (bd->records == NULL) {
5607 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5608 talloc_free(tmp_ctx);
5612 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5613 bd->records->db_id = ctdb_db->db_id;
5614 /* traverse the database collecting all records */
5615 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5616 bd->traverse_error) {
5617 DEBUG(DEBUG_ERR,("Traverse error\n"));
5618 talloc_free(tmp_ctx);
5622 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5625 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5627 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5628 talloc_free(tmp_ctx);
5633 dbhdr.version = DB_VERSION;
5634 dbhdr.timestamp = time(NULL);
5635 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5636 dbhdr.size = bd->len;
5637 if (strlen(argv[0]) >= MAX_DB_NAME) {
5638 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5641 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5642 ret = write(fh, &dbhdr, sizeof(dbhdr));
5644 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5647 ret = write(fh, bd->records, bd->len);
5649 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5658 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5662 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5664 talloc_free(tmp_ctx);
5669 * restore a database from a file
5671 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5674 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5677 struct db_file_header dbhdr;
5678 struct ctdb_db_context *ctdb_db;
5679 struct ctdb_node_map *nodemap=NULL;
5680 struct ctdb_vnn_map *vnnmap=NULL;
5682 struct ctdb_control_wipe_database w;
5684 uint32_t generation;
5689 assert_single_node_only();
5691 if (argc < 1 || argc > 2) {
5692 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5696 fh = open(argv[0], O_RDONLY);
5698 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5699 talloc_free(tmp_ctx);
5703 read(fh, &dbhdr, sizeof(dbhdr));
5704 if (dbhdr.version != DB_VERSION) {
5705 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5707 talloc_free(tmp_ctx);
5711 dbname = discard_const(dbhdr.name);
5713 dbname = discard_const(argv[1]);
5716 outdata.dsize = dbhdr.size;
5717 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5718 if (outdata.dptr == NULL) {
5719 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5721 talloc_free(tmp_ctx);
5724 read(fh, outdata.dptr, outdata.dsize);
5727 tm = localtime(&dbhdr.timestamp);
5728 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5729 printf("Restoring database '%s' from backup @ %s\n",
5733 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5734 if (ctdb_db == NULL) {
5735 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5736 talloc_free(tmp_ctx);
5740 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5742 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5743 talloc_free(tmp_ctx);
5748 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5750 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5751 talloc_free(tmp_ctx);
5755 /* freeze all nodes */
5756 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5757 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5758 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5764 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5765 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5766 talloc_free(tmp_ctx);
5771 generation = vnnmap->generation;
5772 data.dptr = (void *)&generation;
5773 data.dsize = sizeof(generation);
5775 /* start a cluster wide transaction */
5776 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5777 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5779 TIMELIMIT(), false, data,
5782 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5787 w.db_id = ctdb_db->db_id;
5788 w.transaction_id = generation;
5790 data.dptr = (void *)&w;
5791 data.dsize = sizeof(w);
5793 /* wipe all the remote databases. */
5794 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5795 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5797 TIMELIMIT(), false, data,
5800 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5801 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5802 talloc_free(tmp_ctx);
5806 /* push the database */
5807 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5808 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5810 TIMELIMIT(), false, outdata,
5813 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5814 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5815 talloc_free(tmp_ctx);
5819 data.dptr = (void *)&ctdb_db->db_id;
5820 data.dsize = sizeof(ctdb_db->db_id);
5822 /* mark the database as healthy */
5823 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5824 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5826 TIMELIMIT(), false, data,
5829 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5830 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5831 talloc_free(tmp_ctx);
5835 data.dptr = (void *)&generation;
5836 data.dsize = sizeof(generation);
5838 /* commit all the changes */
5839 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5841 TIMELIMIT(), false, data,
5844 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5845 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5846 talloc_free(tmp_ctx);
5851 /* thaw all nodes */
5852 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5853 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5859 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5860 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5861 talloc_free(tmp_ctx);
5866 talloc_free(tmp_ctx);
5871 * dump a database backup from a file
5873 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5875 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5877 struct db_file_header dbhdr;
5881 struct ctdb_rec_data *rec = NULL;
5882 struct ctdb_marshall_buffer *m;
5883 struct ctdb_dump_db_context c;
5885 assert_single_node_only();
5888 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5892 fh = open(argv[0], O_RDONLY);
5894 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5895 talloc_free(tmp_ctx);
5899 read(fh, &dbhdr, sizeof(dbhdr));
5900 if (dbhdr.version != DB_VERSION) {
5901 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5903 talloc_free(tmp_ctx);
5907 outdata.dsize = dbhdr.size;
5908 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5909 if (outdata.dptr == NULL) {
5910 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5912 talloc_free(tmp_ctx);
5915 read(fh, outdata.dptr, outdata.dsize);
5917 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5919 tm = localtime(&dbhdr.timestamp);
5920 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5921 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5922 dbhdr.name, m->db_id, tbuf);
5926 c.printemptyrecords = (bool)options.printemptyrecords;
5927 c.printdatasize = (bool)options.printdatasize;
5928 c.printlmaster = false;
5929 c.printhash = (bool)options.printhash;
5930 c.printrecordflags = (bool)options.printrecordflags;
5932 for (i=0; i < m->count; i++) {
5936 /* we do not want the header splitted, so we pass NULL*/
5937 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5940 ctdb_dumpdb_record(ctdb, key, data, &c);
5943 printf("Dumped %d records\n", i);
5944 talloc_free(tmp_ctx);
5949 * wipe a database from a file
5951 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5954 const char *db_name;
5956 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5958 struct ctdb_db_context *ctdb_db;
5959 struct ctdb_node_map *nodemap = NULL;
5960 struct ctdb_vnn_map *vnnmap = NULL;
5962 struct ctdb_control_wipe_database w;
5964 uint32_t generation;
5967 assert_single_node_only();
5970 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5974 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5978 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5979 if (ctdb_db == NULL) {
5980 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5982 talloc_free(tmp_ctx);
5986 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5989 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5991 talloc_free(tmp_ctx);
5995 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5998 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
6000 talloc_free(tmp_ctx);
6004 /* freeze all nodes */
6005 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6006 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
6007 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
6014 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
6015 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
6016 CTDB_RECOVERY_ACTIVE);
6017 talloc_free(tmp_ctx);
6022 generation = vnnmap->generation;
6023 data.dptr = (void *)&generation;
6024 data.dsize = sizeof(generation);
6026 /* start a cluster wide transaction */
6027 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6028 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
6030 TIMELIMIT(), false, data,
6034 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
6035 "transactions.\n"));
6039 w.db_id = ctdb_db->db_id;
6040 w.transaction_id = generation;
6042 data.dptr = (void *)&w;
6043 data.dsize = sizeof(w);
6045 /* wipe all the remote databases. */
6046 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6047 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
6049 TIMELIMIT(), false, data,
6052 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
6053 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6054 talloc_free(tmp_ctx);
6058 data.dptr = (void *)&ctdb_db->db_id;
6059 data.dsize = sizeof(ctdb_db->db_id);
6061 /* mark the database as healthy */
6062 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6063 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
6065 TIMELIMIT(), false, data,
6068 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
6069 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6070 talloc_free(tmp_ctx);
6074 data.dptr = (void *)&generation;
6075 data.dsize = sizeof(generation);
6077 /* commit all the changes */
6078 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
6080 TIMELIMIT(), false, data,
6083 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
6084 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6085 talloc_free(tmp_ctx);
6089 /* thaw all nodes */
6090 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6091 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
6097 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
6098 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6099 talloc_free(tmp_ctx);
6103 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
6105 talloc_free(tmp_ctx);
6112 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6118 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6119 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6120 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6121 if (ret != 0 || res != 0) {
6122 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6123 talloc_free(tmp_ctx);
6126 write(1, data.dptr, data.dsize);
6127 talloc_free(tmp_ctx);
6132 handler for memory dumps
6134 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6135 TDB_DATA data, void *private_data)
6137 write(1, data.dptr, data.dsize);
6142 dump memory usage on the recovery daemon
6144 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6148 struct srvid_request rd;
6150 rd.pnn = ctdb_get_pnn(ctdb);
6151 rd.srvid = getpid();
6153 /* register a message port for receiveing the reply so that we
6154 can receive the reply
6156 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6159 data.dptr = (uint8_t *)&rd;
6160 data.dsize = sizeof(rd);
6162 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6164 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6168 /* this loop will terminate when we have received the reply */
6170 event_loop_once(ctdb->ev);
6177 send a message to a srvid
6179 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6181 unsigned long srvid;
6189 srvid = strtoul(argv[0], NULL, 0);
6191 data.dptr = (uint8_t *)discard_const(argv[1]);
6192 data.dsize= strlen(argv[1]);
6194 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6196 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6204 handler for msglisten
6206 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6207 TDB_DATA data, void *private_data)
6211 printf("Message received: ");
6212 for (i=0;i<data.dsize;i++) {
6213 printf("%c", data.dptr[i]);
6219 listen for messages on a messageport
6221 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6227 /* register a message port and listen for messages
6229 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6230 printf("Listening for messages on srvid:%d\n", (int)srvid);
6233 event_loop_once(ctdb->ev);
6240 list all nodes in the cluster
6241 we parse the nodes file directly
6243 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6245 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6246 struct pnn_node *pnn_nodes;
6247 struct pnn_node *pnn_node;
6249 assert_single_node_only();
6251 pnn_nodes = read_nodes_file(mem_ctx);
6252 if (pnn_nodes == NULL) {
6253 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6254 talloc_free(mem_ctx);
6258 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6259 const char *addr = ctdb_addr_to_str(&pnn_node->addr);
6260 if (options.machinereadable){
6261 printf(":%d:%s:\n", pnn_node->pnn, addr);
6263 printf("%s\n", addr);
6266 talloc_free(mem_ctx);
6272 reload the nodes file on the local node
6274 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6278 struct ctdb_node_map *nodemap=NULL;
6280 assert_single_node_only();
6282 mypnn = ctdb_get_pnn(ctdb);
6284 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6286 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6290 /* reload the nodes file on all remote nodes */
6291 for (i=0;i<nodemap->num;i++) {
6292 if (nodemap->nodes[i].pnn == mypnn) {
6295 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6296 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6297 nodemap->nodes[i].pnn);
6299 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6303 /* reload the nodes file on the local node */
6304 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6305 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6307 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6310 /* initiate a recovery */
6311 control_recover(ctdb, argc, argv);
6317 static const struct {
6319 int (*fn)(struct ctdb_context *, int, const char **);
6321 bool without_daemon; /* can be run without daemon running ? */
6324 } ctdb_commands[] = {
6325 { "version", control_version, true, true, "show version of ctdb" },
6326 { "status", control_status, true, false, "show node status" },
6327 { "uptime", control_uptime, true, false, "show node uptime" },
6328 { "ping", control_ping, true, false, "ping all nodes" },
6329 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6330 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6331 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6332 { "listvars", control_listvars, true, false, "list tunable variables"},
6333 { "statistics", control_statistics, false, false, "show statistics" },
6334 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6335 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6336 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6337 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6338 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6339 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6340 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6341 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6342 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6343 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6344 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6345 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6346 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6347 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6348 { "lvs", control_lvs, true, false, "show lvs configuration" },
6349 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6350 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6351 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6352 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6353 { "getdebug", control_getdebug, true, false, "get debug level" },
6354 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6355 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6356 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6357 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6358 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6359 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6360 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6361 { "disable", control_disable, true, false, "disable a nodes public IP" },
6362 { "enable", control_enable, true, false, "enable a nodes public IP" },
6363 { "stop", control_stop, true, false, "stop a node" },
6364 { "continue", control_continue, true, false, "re-start a stopped node" },
6365 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6366 { "unban", control_unban, true, false, "unban a node" },
6367 { "showban", control_showban, true, false, "show ban information"},
6368 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6369 { "recover", control_recover, true, false, "force recovery" },
6370 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6371 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6372 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6373 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6374 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6375 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6376 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6377 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6378 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6380 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6382 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6383 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6384 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6385 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6386 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6387 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6388 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6389 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6390 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6391 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6392 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6393 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6394 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6395 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6396 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6397 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6398 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6399 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6400 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6401 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6402 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6403 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6404 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6405 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6406 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6407 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6408 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6409 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6410 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6411 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6412 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6413 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6414 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6415 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6416 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6417 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6418 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6419 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6420 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6421 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6422 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6423 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6424 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6425 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6426 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6427 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6428 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6429 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6430 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6436 static void usage(void)
6440 "Usage: ctdb [options] <control>\n" \
6442 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6443 " -Y generate machinereadable output\n"
6444 " -v generate verbose output\n"
6445 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6446 printf("Controls:\n");
6447 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6448 printf(" %-15s %-27s %s\n",
6449 ctdb_commands[i].name,
6450 ctdb_commands[i].args?ctdb_commands[i].args:"",
6451 ctdb_commands[i].msg);
6457 static void ctdb_alarm(int sig)
6459 printf("Maximum runtime exceeded - exiting\n");
6466 int main(int argc, const char *argv[])
6468 struct ctdb_context *ctdb;
6469 char *nodestring = NULL;
6470 struct poptOption popt_options[] = {
6473 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6474 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6475 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6476 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6477 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6478 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6479 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6480 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6481 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6482 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6486 const char **extra_argv;
6490 struct event_context *ev;
6491 const char *control;
6495 /* set some defaults */
6496 options.maxruntime = 0;
6497 options.timelimit = 10;
6498 options.pnn = CTDB_CURRENT_NODE;
6500 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6502 while ((opt = poptGetNextOpt(pc)) != -1) {
6505 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6506 poptBadOption(pc, 0), poptStrerror(opt)));
6511 /* setup the remaining options for the main program to use */
6512 extra_argv = poptGetArgs(pc);
6515 while (extra_argv[extra_argc]) extra_argc++;
6518 if (extra_argc < 1) {
6522 if (options.maxruntime == 0) {
6523 const char *ctdb_timeout;
6524 ctdb_timeout = getenv("CTDB_TIMEOUT");
6525 if (ctdb_timeout != NULL) {
6526 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6528 /* default timeout is 120 seconds */
6529 options.maxruntime = 120;
6533 signal(SIGALRM, ctdb_alarm);
6534 alarm(options.maxruntime);
6536 control = extra_argv[0];
6538 /* Default value for CTDB_BASE - don't override */
6539 setenv("CTDB_BASE", ETCDIR "/ctdb", 0);
6541 ev = event_context_init(NULL);
6543 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6547 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6548 if (strcmp(control, ctdb_commands[i].name) == 0) {
6553 if (i == ARRAY_SIZE(ctdb_commands)) {
6554 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6558 if (ctdb_commands[i].without_daemon == true) {
6559 if (nodestring != NULL) {
6560 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6563 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6566 /* initialise ctdb */
6567 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6570 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6574 /* setup the node number(s) to contact */
6575 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6576 &options.nodes, &options.pnn)) {
6580 if (options.pnn == CTDB_CURRENT_NODE) {
6581 options.pnn = options.nodes[0];
6584 if (ctdb_commands[i].auto_all &&
6585 ((options.pnn == CTDB_BROADCAST_ALL) ||
6586 (options.pnn == CTDB_MULTICAST))) {
6590 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6591 options.pnn = options.nodes[j];
6592 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6595 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6600 (void)poptFreeContext(pc);