4 Copyright (C) Andrew Tridgell 2007
5 Copyright (C) Ronnie Sahlberg 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "system/network.h"
25 #include "system/locale.h"
28 #include "../include/ctdb_version.h"
29 #include "../include/ctdb_client.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
33 #include "lib/util/dlinklist.h"
35 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
36 #define ERR_NONODE 21 /* node does not exist */
37 #define ERR_DISNODE 22 /* node is disconnected */
39 static void usage(void);
48 int printemptyrecords;
55 #define LONGTIMEOUT options.timelimit*10
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
60 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
62 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
66 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
67 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
68 "Out of memory in " __location__ )); \
72 static uint32_t getpnn(struct ctdb_context *ctdb)
74 if ((options.pnn == CTDB_BROADCAST_ALL) ||
75 (options.pnn == CTDB_MULTICAST)) {
77 ("Cannot get PNN for node %u\n", options.pnn));
81 if (options.pnn == CTDB_CURRENT_NODE) {
82 return ctdb_get_pnn(ctdb);
88 static void assert_single_node_only(void)
90 if ((options.pnn == CTDB_BROADCAST_ALL) ||
91 (options.pnn == CTDB_MULTICAST)) {
93 ("This control can not be applied to multiple PNNs\n"));
98 /* Pretty print the flags to a static buffer in human-readable format.
99 * This never returns NULL!
101 static const char *pretty_print_flags(uint32_t flags)
104 static const struct {
108 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
109 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
110 { NODE_FLAGS_BANNED, "BANNED" },
111 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
112 { NODE_FLAGS_DELETED, "DELETED" },
113 { NODE_FLAGS_STOPPED, "STOPPED" },
114 { NODE_FLAGS_INACTIVE, "INACTIVE" },
116 static char flags_str[512]; /* Big enough to contain all flag names */
119 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
120 if (flags & flag_names[j].flag) {
121 if (flags_str[0] == '\0') {
122 (void) strcpy(flags_str, flag_names[j].name);
124 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
125 (void) strncat(flags_str, flag_names[j].name,
126 sizeof(flags_str)-1);
130 if (flags_str[0] == '\0') {
131 (void) strcpy(flags_str, "OK");
137 static int h2i(char h)
139 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
140 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
144 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
147 TDB_DATA key = {NULL, 0};
151 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
156 key.dptr = talloc_size(mem_ctx, key.dsize);
158 for (i=0; i < len/2; i++) {
159 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
164 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
165 * that are disconnected or deleted. If dd_ok is true those nodes are
166 * included in the output list of nodes. If dd_ok is false, those
167 * nodes are filtered from the "all" case and cause an error if
168 * explicitly specified.
170 static bool parse_nodestring(struct ctdb_context *ctdb,
172 const char * nodestring,
173 uint32_t current_pnn,
178 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
181 struct ctdb_node_map *nodemap;
186 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
188 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
189 talloc_free(tmp_ctx);
193 if (nodestring != NULL) {
194 *nodes = talloc_array(mem_ctx, uint32_t, 0);
195 if (*nodes == NULL) {
201 if (strcmp(nodestring, "all") == 0) {
202 *pnn_mode = CTDB_BROADCAST_ALL;
205 for (i = 0; i < nodemap->num; i++) {
206 if ((nodemap->nodes[i].flags &
207 (NODE_FLAGS_DISCONNECTED |
208 NODE_FLAGS_DELETED)) && !dd_ok) {
211 *nodes = talloc_realloc(mem_ctx, *nodes,
213 if (*nodes == NULL) {
223 ns = talloc_strdup(tmp_ctx, nodestring);
224 tok = strtok(ns, ",");
225 while (tok != NULL) {
228 i = (uint32_t)strtoul(tok, &endptr, 0);
229 if (i == 0 && tok == endptr) {
231 ("Invalid node %s\n", tok));
232 talloc_free(tmp_ctx);
235 if (i >= nodemap->num) {
236 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
237 talloc_free(tmp_ctx);
240 if ((nodemap->nodes[i].flags &
241 (NODE_FLAGS_DISCONNECTED |
242 NODE_FLAGS_DELETED)) && !dd_ok) {
243 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
244 talloc_free(tmp_ctx);
247 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
248 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
249 talloc_free(tmp_ctx);
253 *nodes = talloc_realloc(mem_ctx, *nodes,
255 if (*nodes == NULL) {
262 tok = strtok(NULL, ",");
267 *pnn_mode = (*nodes)[0];
269 *pnn_mode = CTDB_MULTICAST;
273 /* default - no nodes specified */
274 *nodes = talloc_array(mem_ctx, uint32_t, 1);
275 if (*nodes == NULL) {
278 *pnn_mode = CTDB_CURRENT_NODE;
280 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
285 talloc_free(tmp_ctx);
289 talloc_free(tmp_ctx);
294 check if a database exists
296 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
297 uint32_t *dbid, const char **dbname, uint8_t *flags)
300 struct ctdb_dbid_map *dbmap=NULL;
301 bool dbid_given = false, found = false;
303 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
306 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
308 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
312 if (strncmp(dbarg, "0x", 2) == 0) {
313 id = strtoul(dbarg, NULL, 0);
317 for(i=0; i<dbmap->num; i++) {
319 if (id == dbmap->dbs[i].dbid) {
324 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
326 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
330 if (strcmp(name, dbarg) == 0) {
331 id = dbmap->dbs[i].dbid;
338 if (found && dbid_given && dbname != NULL) {
339 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
341 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
348 if (dbid) *dbid = id;
349 if (dbname) *dbname = talloc_strdup(ctdb, name);
350 if (flags) *flags = dbmap->dbs[i].flags;
352 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
356 talloc_free(tmp_ctx);
361 see if a process exists
363 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
371 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
372 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
376 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
378 printf("%u:%u exists\n", pnn, pid);
380 printf("%u:%u does not exist\n", pnn, pid);
386 display statistics structure
388 static void show_statistics(struct ctdb_statistics *s, int show_header)
390 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
392 const char *prefix=NULL;
394 int tmp, days, hours, minutes, seconds;
399 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
400 STATISTICS_FIELD(num_clients),
401 STATISTICS_FIELD(frozen),
402 STATISTICS_FIELD(recovering),
403 STATISTICS_FIELD(num_recoveries),
404 STATISTICS_FIELD(client_packets_sent),
405 STATISTICS_FIELD(client_packets_recv),
406 STATISTICS_FIELD(node_packets_sent),
407 STATISTICS_FIELD(node_packets_recv),
408 STATISTICS_FIELD(keepalive_packets_sent),
409 STATISTICS_FIELD(keepalive_packets_recv),
410 STATISTICS_FIELD(node.req_call),
411 STATISTICS_FIELD(node.reply_call),
412 STATISTICS_FIELD(node.req_dmaster),
413 STATISTICS_FIELD(node.reply_dmaster),
414 STATISTICS_FIELD(node.reply_error),
415 STATISTICS_FIELD(node.req_message),
416 STATISTICS_FIELD(node.req_control),
417 STATISTICS_FIELD(node.reply_control),
418 STATISTICS_FIELD(client.req_call),
419 STATISTICS_FIELD(client.req_message),
420 STATISTICS_FIELD(client.req_control),
421 STATISTICS_FIELD(timeouts.call),
422 STATISTICS_FIELD(timeouts.control),
423 STATISTICS_FIELD(timeouts.traverse),
424 STATISTICS_FIELD(locks.num_calls),
425 STATISTICS_FIELD(locks.num_current),
426 STATISTICS_FIELD(locks.num_pending),
427 STATISTICS_FIELD(locks.num_failed),
428 STATISTICS_FIELD(total_calls),
429 STATISTICS_FIELD(pending_calls),
430 STATISTICS_FIELD(childwrite_calls),
431 STATISTICS_FIELD(pending_childwrite_calls),
432 STATISTICS_FIELD(memory_used),
433 STATISTICS_FIELD(max_hop_count),
434 STATISTICS_FIELD(total_ro_delegations),
435 STATISTICS_FIELD(total_ro_revokes),
438 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
447 if (options.machinereadable){
449 printf("CTDB version:");
450 printf("Current time of statistics:");
451 printf("Statistics collected since:");
452 for (i=0;i<ARRAY_SIZE(fields);i++) {
453 printf("%s:", fields[i].name);
455 printf("num_reclock_ctdbd_latency:");
456 printf("min_reclock_ctdbd_latency:");
457 printf("avg_reclock_ctdbd_latency:");
458 printf("max_reclock_ctdbd_latency:");
460 printf("num_reclock_recd_latency:");
461 printf("min_reclock_recd_latency:");
462 printf("avg_reclock_recd_latency:");
463 printf("max_reclock_recd_latency:");
465 printf("num_call_latency:");
466 printf("min_call_latency:");
467 printf("avg_call_latency:");
468 printf("max_call_latency:");
470 printf("num_lockwait_latency:");
471 printf("min_lockwait_latency:");
472 printf("avg_lockwait_latency:");
473 printf("max_lockwait_latency:");
475 printf("num_childwrite_latency:");
476 printf("min_childwrite_latency:");
477 printf("avg_childwrite_latency:");
478 printf("max_childwrite_latency:");
481 printf("%d:", CTDB_VERSION);
482 printf("%d:", (int)s->statistics_current_time.tv_sec);
483 printf("%d:", (int)s->statistics_start_time.tv_sec);
484 for (i=0;i<ARRAY_SIZE(fields);i++) {
485 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
487 printf("%d:", s->reclock.ctdbd.num);
488 printf("%.6f:", s->reclock.ctdbd.min);
489 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
490 printf("%.6f:", s->reclock.ctdbd.max);
492 printf("%d:", s->reclock.recd.num);
493 printf("%.6f:", s->reclock.recd.min);
494 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
495 printf("%.6f:", s->reclock.recd.max);
497 printf("%d:", s->call_latency.num);
498 printf("%.6f:", s->call_latency.min);
499 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
500 printf("%.6f:", s->call_latency.max);
502 printf("%d:", s->childwrite_latency.num);
503 printf("%.6f:", s->childwrite_latency.min);
504 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
505 printf("%.6f:", s->childwrite_latency.max);
508 printf("CTDB version %u\n", CTDB_VERSION);
509 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
510 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
512 for (i=0;i<ARRAY_SIZE(fields);i++) {
513 if (strchr(fields[i].name, '.')) {
514 preflen = strcspn(fields[i].name, ".")+1;
515 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
516 prefix = fields[i].name;
517 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
522 printf(" %*s%-22s%*s%10u\n",
524 fields[i].name+preflen,
526 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
528 printf(" hop_count_buckets:");
529 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
530 printf(" %d", s->hop_count_bucket[i]);
533 printf(" lock_buckets:");
534 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
535 printf(" %d", s->locks.buckets[i]);
538 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "locks_latency MIN/AVG/MAX", s->locks.latency.min, s->locks.latency.num?s->locks.latency.total/s->locks.latency.num:0.0, s->locks.latency.max, s->locks.latency.num);
540 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "reclock_ctdbd MIN/AVG/MAX", s->reclock.ctdbd.min, s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0, s->reclock.ctdbd.max, s->reclock.ctdbd.num);
542 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "reclock_recd MIN/AVG/MAX", s->reclock.recd.min, s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0, s->reclock.recd.max, s->reclock.recd.num);
544 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "call_latency MIN/AVG/MAX", s->call_latency.min, s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0, s->call_latency.max, s->call_latency.num);
545 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "childwrite_latency MIN/AVG/MAX", s->childwrite_latency.min, s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0, s->childwrite_latency.max, s->childwrite_latency.num);
548 talloc_free(tmp_ctx);
552 display remote ctdb statistics combined from all nodes
554 static int control_statistics_all(struct ctdb_context *ctdb)
557 struct ctdb_statistics statistics;
561 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
562 CTDB_NO_MEMORY(ctdb, nodes);
564 ZERO_STRUCT(statistics);
566 for (i=0;i<num_nodes;i++) {
567 struct ctdb_statistics s1;
569 uint32_t *v1 = (uint32_t *)&s1;
570 uint32_t *v2 = (uint32_t *)&statistics;
572 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
573 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
575 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
578 for (j=0;j<num_ints;j++) {
581 statistics.max_hop_count =
582 MAX(statistics.max_hop_count, s1.max_hop_count);
583 statistics.call_latency.max =
584 MAX(statistics.call_latency.max, s1.call_latency.max);
587 printf("Gathered statistics for %u nodes\n", num_nodes);
588 show_statistics(&statistics, 1);
593 display remote ctdb statistics
595 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
598 struct ctdb_statistics statistics;
600 if (options.pnn == CTDB_BROADCAST_ALL) {
601 return control_statistics_all(ctdb);
604 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
606 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
609 show_statistics(&statistics, 1);
615 reset remote ctdb statistics
617 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
621 ret = ctdb_statistics_reset(ctdb, options.pnn);
623 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
631 display remote ctdb rolling statistics
633 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
636 struct ctdb_statistics_wire *stats;
637 int i, num_records = -1;
639 assert_single_node_only();
642 num_records = atoi(argv[0]) - 1;
645 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
647 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
650 for (i=0;i<stats->num;i++) {
651 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
654 show_statistics(&stats->stats[i], i==0);
655 if (i == num_records) {
664 display remote ctdb db statistics
666 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
668 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
669 struct ctdb_db_statistics *dbstat;
679 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
683 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
685 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
686 talloc_free(tmp_ctx);
690 printf("DB Statistics: %s\n", argv[0]);
691 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
692 dbstat->db_ro_delegations);
693 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
694 dbstat->db_ro_delegations);
695 printf(" %s\n", "locks");
696 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
697 dbstat->locks.num_calls);
698 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
699 dbstat->locks.num_failed);
700 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
701 dbstat->locks.num_current);
702 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
703 dbstat->locks.num_pending);
704 printf(" %s", "hop_count_buckets:");
705 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
706 printf(" %d", dbstat->hop_count_bucket[i]);
709 printf(" %s", "lock_buckets:");
710 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
711 printf(" %d", dbstat->locks.buckets[i]);
714 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
715 "locks_latency MIN/AVG/MAX",
716 dbstat->locks.latency.min,
717 (dbstat->locks.latency.num ?
718 dbstat->locks.latency.total /dbstat->locks.latency.num :
720 dbstat->locks.latency.max,
721 dbstat->locks.latency.num);
723 for (i=0; i<dbstat->num_hot_keys; i++) {
724 if (dbstat->hot_keys[i].count > 0) {
728 dbstat->num_hot_keys = num_hot_keys;
730 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
731 for (i = 0; i < dbstat->num_hot_keys; i++) {
733 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
734 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
735 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
740 talloc_free(tmp_ctx);
745 display uptime of remote node
747 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
750 struct ctdb_uptime *uptime = NULL;
751 int tmp, days, hours, minutes, seconds;
753 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
755 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
759 if (options.machinereadable){
760 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
761 printf(":%u:%u:%u:%lf\n",
762 (unsigned int)uptime->current_time.tv_sec,
763 (unsigned int)uptime->ctdbd_start_time.tv_sec,
764 (unsigned int)uptime->last_recovery_finished.tv_sec,
765 timeval_delta(&uptime->last_recovery_finished,
766 &uptime->last_recovery_started)
771 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
773 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
781 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
783 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
791 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
793 printf("Duration of last recovery/failover: %lf seconds\n",
794 timeval_delta(&uptime->last_recovery_finished,
795 &uptime->last_recovery_started));
801 show the PNN of the current node
803 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
807 mypnn = getpnn(ctdb);
809 printf("PNN:%d\n", mypnn);
815 struct pnn_node *next, *prev;
820 static struct pnn_node *read_pnn_node_file(TALLOC_CTX *mem_ctx,
826 struct pnn_node *pnn_nodes = NULL;
827 struct pnn_node *pnn_node;
829 lines = file_lines_load(file, &nlines, mem_ctx);
833 for (i=0, pnn=0; i<nlines; i++) {
837 /* strip leading spaces */
838 while((*node == ' ') || (*node == '\t')) {
845 if (strcmp(node, "") == 0) {
848 pnn_node = talloc(mem_ctx, struct pnn_node);
849 pnn_node->pnn = pnn++;
851 if (!parse_ip(node, NULL, 0, &pnn_node->addr)) {
853 ("Invalid IP address '%s' in file %s\n",
855 /* Caller will free mem_ctx */
859 DLIST_ADD_END(pnn_nodes, pnn_node, NULL);
865 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
867 const char *nodes_list;
869 /* read the nodes file */
870 nodes_list = getenv("CTDB_NODES");
871 if (nodes_list == NULL) {
872 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
873 getenv("CTDB_BASE"));
874 if (nodes_list == NULL) {
875 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
880 return read_pnn_node_file(mem_ctx, nodes_list);
884 show the PNN of the current node
885 discover the pnn by loading the nodes file and try to bind to all
886 addresses one at a time until the ip address is found.
888 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
890 TALLOC_CTX *mem_ctx = talloc_new(NULL);
891 struct pnn_node *pnn_nodes;
892 struct pnn_node *pnn_node;
894 assert_single_node_only();
896 pnn_nodes = read_nodes_file(mem_ctx);
897 if (pnn_nodes == NULL) {
898 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
899 talloc_free(mem_ctx);
903 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
904 if (ctdb_sys_have_ip(&pnn_node->addr)) {
905 printf("PNN:%d\n", pnn_node->pnn);
906 talloc_free(mem_ctx);
911 printf("Failed to detect which PNN this node is\n");
912 talloc_free(mem_ctx);
916 /* Helpers for ctdb status
918 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
920 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
924 if (node->flags == 0) {
925 struct ctdb_control_get_ifaces *ifaces;
927 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
928 tmp_ctx, &ifaces) == 0) {
929 for (j=0; j < ifaces->num; j++) {
930 if (ifaces->ifaces[j].link_state != 0) {
938 talloc_free(tmp_ctx);
943 static void control_status_header_machine(void)
945 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
946 ":Inactive:PartiallyOnline:ThisNode:\n");
949 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
950 struct ctdb_node_and_flags *node)
952 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
953 ctdb_addr_to_str(&node->addr),
954 !!(node->flags&NODE_FLAGS_DISCONNECTED),
955 !!(node->flags&NODE_FLAGS_BANNED),
956 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
957 !!(node->flags&NODE_FLAGS_UNHEALTHY),
958 !!(node->flags&NODE_FLAGS_STOPPED),
959 !!(node->flags&NODE_FLAGS_INACTIVE),
960 is_partially_online(ctdb, node) ? 1 : 0,
961 (node->pnn == mypnn)?'Y':'N');
966 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
967 struct ctdb_node_and_flags *node)
969 printf("pnn:%d %-16s %s%s\n", node->pnn,
970 ctdb_addr_to_str(&node->addr),
971 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
972 node->pnn == mypnn?" (THIS NODE)":"");
978 display remote ctdb status
980 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
982 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
984 struct ctdb_vnn_map *vnnmap=NULL;
985 struct ctdb_node_map *nodemap=NULL;
986 uint32_t recmode, recmaster, mypnn;
987 int num_deleted_nodes = 0;
990 mypnn = getpnn(ctdb);
992 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
994 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
995 talloc_free(tmp_ctx);
999 if (options.machinereadable) {
1000 control_status_header_machine();
1001 for (i=0;i<nodemap->num;i++) {
1002 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1005 (void) control_status_1_machine(ctdb, mypnn,
1006 &nodemap->nodes[i]);
1008 talloc_free(tmp_ctx);
1012 for (i=0; i<nodemap->num; i++) {
1013 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1014 num_deleted_nodes++;
1017 if (num_deleted_nodes == 0) {
1018 printf("Number of nodes:%d\n", nodemap->num);
1020 printf("Number of nodes:%d (including %d deleted nodes)\n",
1021 nodemap->num, num_deleted_nodes);
1023 for(i=0;i<nodemap->num;i++){
1024 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1027 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1030 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1032 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1033 talloc_free(tmp_ctx);
1036 if (vnnmap->generation == INVALID_GENERATION) {
1037 printf("Generation:INVALID\n");
1039 printf("Generation:%d\n",vnnmap->generation);
1041 printf("Size:%d\n",vnnmap->size);
1042 for(i=0;i<vnnmap->size;i++){
1043 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1046 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1048 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1049 talloc_free(tmp_ctx);
1052 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1054 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1056 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1057 talloc_free(tmp_ctx);
1060 printf("Recovery master:%d\n",recmaster);
1062 talloc_free(tmp_ctx);
1066 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1068 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1070 struct ctdb_node_map *nodemap=NULL;
1072 uint32_t pnn_mode, mypnn;
1078 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1079 options.pnn, true, &nodes, &pnn_mode)) {
1083 if (options.machinereadable) {
1084 control_status_header_machine();
1085 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1086 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1089 mypnn = getpnn(ctdb);
1091 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1093 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1094 talloc_free(tmp_ctx);
1100 for (i = 0; i < talloc_array_length(nodes); i++) {
1101 if (options.machinereadable) {
1102 ret |= control_status_1_machine(ctdb, mypnn,
1103 &nodemap->nodes[nodes[i]]);
1105 ret |= control_status_1_human(ctdb, mypnn,
1106 &nodemap->nodes[nodes[i]]);
1110 talloc_free(tmp_ctx);
1114 static struct pnn_node *read_natgw_nodes_file(struct ctdb_context *ctdb,
1115 TALLOC_CTX *mem_ctx)
1117 const char *natgw_list;
1118 struct pnn_node *natgw_nodes = NULL;
1120 natgw_list = getenv("CTDB_NATGW_NODES");
1121 if (natgw_list == NULL) {
1122 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1123 getenv("CTDB_BASE"));
1124 if (natgw_list == NULL) {
1125 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1129 /* The PNNs will be junk but they're not used */
1130 natgw_nodes = read_pnn_node_file(mem_ctx, natgw_list);
1131 if (natgw_nodes == NULL) {
1133 ("Failed to load natgw node list '%s'\n", natgw_list));
1139 /* talloc off the existing nodemap... */
1140 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1142 return talloc_zero_size(nodemap,
1143 offsetof(struct ctdb_node_map, nodes) +
1144 nodemap->num * sizeof(struct ctdb_node_and_flags));
1147 static struct ctdb_node_map *
1148 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1149 struct ctdb_node_map *nodemap,
1150 struct pnn_node *nodes)
1154 struct ctdb_node_map *ret;
1156 ret = talloc_nodemap(nodemap);
1157 CTDB_NO_MEMORY_NULL(ctdb, ret);
1161 for (i = 0; i < nodemap->num; i++) {
1162 for(n = nodes; n != NULL ; n = n->next) {
1163 if (ctdb_same_ip(&n->addr,
1164 &nodemap->nodes[i].addr)) {
1172 ret->nodes[ret->num] = nodemap->nodes[i];
1179 static struct ctdb_node_map *
1180 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1181 struct ctdb_node_map *nodemap,
1182 uint32_t required_capabilities)
1185 uint32_t capabilities;
1186 struct ctdb_node_map *ret;
1188 ret = talloc_nodemap(nodemap);
1189 CTDB_NO_MEMORY_NULL(ctdb, ret);
1193 for (i = 0; i < nodemap->num; i++) {
1196 /* Disconnected nodes have no capabilities! */
1197 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1201 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1202 nodemap->nodes[i].pnn,
1205 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1206 nodemap->nodes[i].pnn));
1210 if (!(capabilities & required_capabilities)) {
1214 ret->nodes[ret->num] = nodemap->nodes[i];
1221 static struct ctdb_node_map *
1222 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1223 struct ctdb_node_map *nodemap,
1224 uint32_t flags_mask)
1227 struct ctdb_node_map *ret;
1229 ret = talloc_nodemap(nodemap);
1230 CTDB_NO_MEMORY_NULL(ctdb, ret);
1234 for (i = 0; i < nodemap->num; i++) {
1235 if (nodemap->nodes[i].flags & flags_mask) {
1239 ret->nodes[ret->num] = nodemap->nodes[i];
1247 display the list of nodes belonging to this natgw configuration
1249 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1251 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1253 struct pnn_node *natgw_nodes = NULL;
1254 struct ctdb_node_map *orig_nodemap=NULL;
1255 struct ctdb_node_map *cnodemap, *nodemap;
1256 uint32_t mypnn, pnn;
1259 /* When we have some nodes that could be the NATGW, make a
1260 * series of attempts to find the first node that doesn't have
1261 * certain status flags set.
1263 uint32_t exclude_flags[] = {
1264 /* Look for a nice healthy node */
1265 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1266 /* If not found, an UNHEALTHY/BANNED node will do */
1267 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1268 /* If not found, a STOPPED node will do */
1269 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1273 /* read the natgw nodes file into a linked list */
1274 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1275 if (natgw_nodes == NULL) {
1280 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1281 tmp_ctx, &orig_nodemap);
1283 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1284 talloc_free(tmp_ctx);
1288 /* Get a nodemap that includes only the nodes in the NATGW
1290 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1291 if (nodemap == NULL) {
1296 /* Get a nodemap that includes only the nodes with the NATGW
1298 cnodemap = filter_nodemap_by_capabilities(ctdb, nodemap,
1300 if (cnodemap == NULL) {
1305 ret = 2; /* matches ENOENT */
1308 for (i = 0; exclude_flags[i] != 0; i++) {
1309 struct ctdb_node_map *t =
1310 filter_nodemap_by_flags(ctdb, cnodemap,
1319 pnn = t->nodes[0].pnn;
1320 ip = ctdb_addr_to_str(&t->nodes[0].addr);
1326 if (options.machinereadable) {
1327 printf(":Node:IP:\n");
1328 printf(":%d:%s:\n", pnn, ip);
1330 printf("%d %s\n", pnn, ip);
1333 /* print the pruned list of nodes belonging to this natgw list */
1334 mypnn = getpnn(ctdb);
1335 if (options.machinereadable) {
1336 control_status_header_machine();
1338 printf("Number of nodes:%d\n", nodemap->num);
1340 for(i=0;i<nodemap->num;i++){
1341 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1344 if (options.machinereadable) {
1345 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1347 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1352 talloc_free(tmp_ctx);
1357 display the status of the scripts for monitoring (or other events)
1359 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1360 enum ctdb_eventscript_call type)
1362 struct ctdb_scripts_wire *script_status;
1365 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1367 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1371 if (script_status == NULL) {
1372 if (!options.machinereadable) {
1373 printf("%s cycle never run\n",
1374 ctdb_eventscript_call_names[type]);
1379 if (!options.machinereadable) {
1381 for (i=0; i<script_status->num_scripts; i++) {
1382 if (script_status->scripts[i].status != -ENOEXEC) {
1386 printf("%d scripts were executed last %s cycle\n",
1388 ctdb_eventscript_call_names[type]);
1390 for (i=0; i<script_status->num_scripts; i++) {
1391 const char *status = NULL;
1393 switch (script_status->scripts[i].status) {
1395 status = "TIMEDOUT";
1398 status = "DISABLED";
1404 if (script_status->scripts[i].status > 0)
1408 if (options.machinereadable) {
1409 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1410 ctdb_eventscript_call_names[type],
1411 script_status->scripts[i].name,
1412 script_status->scripts[i].status,
1414 (long)script_status->scripts[i].start.tv_sec,
1415 (long)script_status->scripts[i].start.tv_usec,
1416 (long)script_status->scripts[i].finished.tv_sec,
1417 (long)script_status->scripts[i].finished.tv_usec,
1418 script_status->scripts[i].output);
1422 printf("%-20s Status:%s ",
1423 script_status->scripts[i].name, status);
1425 /* Some other error, eg from stat. */
1426 printf("%-20s Status:CANNOT RUN (%s)",
1427 script_status->scripts[i].name,
1428 strerror(-script_status->scripts[i].status));
1430 if (script_status->scripts[i].status >= 0) {
1431 printf("Duration:%.3lf ",
1432 timeval_delta(&script_status->scripts[i].finished,
1433 &script_status->scripts[i].start));
1435 if (script_status->scripts[i].status != -ENOEXEC) {
1437 ctime(&script_status->scripts[i].start.tv_sec));
1438 if (script_status->scripts[i].status != 0) {
1439 printf(" OUTPUT:%s\n",
1440 script_status->scripts[i].output);
1450 static int control_scriptstatus(struct ctdb_context *ctdb,
1451 int argc, const char **argv)
1454 enum ctdb_eventscript_call type, min, max;
1458 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1463 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1467 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1468 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1474 if (type == CTDB_EVENT_MAX) {
1475 if (strcmp(arg, "all") == 0) {
1477 max = CTDB_EVENT_MAX;
1479 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1484 if (options.machinereadable) {
1485 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1488 for (type = min; type < max; type++) {
1489 ret = control_one_scriptstatus(ctdb, type);
1499 enable an eventscript
1501 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1509 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1511 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1519 disable an eventscript
1521 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1529 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1531 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1539 display the pnn of the recovery master
1541 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1546 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1548 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1551 printf("%d\n",recmaster);
1557 add a tickle to a public address
1559 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1561 struct ctdb_tcp_connection t;
1565 assert_single_node_only();
1571 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1572 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1575 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1576 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1580 data.dptr = (uint8_t *)&t;
1581 data.dsize = sizeof(t);
1583 /* tell all nodes about this tcp connection */
1584 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1585 0, data, ctdb, NULL, NULL, NULL, NULL);
1587 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1596 delete a tickle from a node
1598 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1600 struct ctdb_tcp_connection t;
1604 assert_single_node_only();
1610 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1611 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1614 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1615 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1619 data.dptr = (uint8_t *)&t;
1620 data.dsize = sizeof(t);
1622 /* tell all nodes about this tcp connection */
1623 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1624 0, data, ctdb, NULL, NULL, NULL, NULL);
1626 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1635 get a list of all tickles for this pnn
1637 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1639 struct ctdb_control_tcp_tickle_list *list;
1640 ctdb_sock_addr addr;
1644 assert_single_node_only();
1651 port = atoi(argv[1]);
1654 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1655 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1659 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1661 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1665 if (options.machinereadable){
1666 printf(":source ip:port:destination ip:port:\n");
1667 for (i=0;i<list->tickles.num;i++) {
1668 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1671 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1672 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1675 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1676 printf("Num tickles:%u\n", list->tickles.num);
1677 for (i=0;i<list->tickles.num;i++) {
1678 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1681 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1682 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));
1692 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1694 struct ctdb_all_public_ips *ips;
1695 struct ctdb_public_ip ip;
1698 uint32_t disable_time;
1700 struct ctdb_node_map *nodemap=NULL;
1701 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1704 data.dptr = (uint8_t*)&disable_time;
1705 data.dsize = sizeof(disable_time);
1706 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1708 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1714 /* read the public ip list from the node */
1715 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1717 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1718 talloc_free(tmp_ctx);
1722 for (i=0;i<ips->num;i++) {
1723 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1728 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1729 pnn, ctdb_addr_to_str(addr)));
1730 talloc_free(tmp_ctx);
1737 data.dptr = (uint8_t *)&ip;
1738 data.dsize = sizeof(ip);
1740 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1742 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1743 talloc_free(tmp_ctx);
1747 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1748 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1755 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1756 talloc_free(tmp_ctx);
1760 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1762 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1763 talloc_free(tmp_ctx);
1767 /* update the recovery daemon so it now knows to expect the new
1768 node assignment for this ip.
1770 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1772 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1776 talloc_free(tmp_ctx);
1782 * scans all other nodes and returns a pnn for another node that can host this
1786 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1788 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1789 struct ctdb_all_public_ips *ips;
1790 struct ctdb_node_map *nodemap=NULL;
1793 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1795 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1796 talloc_free(tmp_ctx);
1800 for(i=0;i<nodemap->num;i++){
1801 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1804 if (nodemap->nodes[i].pnn == options.pnn) {
1808 /* read the public ip list from this node */
1809 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1811 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1815 for (j=0;j<ips->num;j++) {
1816 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1817 talloc_free(tmp_ctx);
1818 return nodemap->nodes[i].pnn;
1824 talloc_free(tmp_ctx);
1828 /* If pnn is -1 then try to find a node to move IP to... */
1829 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1831 bool pnn_specified = (pnn == -1 ? false : true);
1834 while (retries < 5) {
1835 if (!pnn_specified) {
1836 pnn = find_other_host_for_public_ip(ctdb, addr);
1841 ("Trying to move public IP to node %u\n", pnn));
1844 if (move_ip(ctdb, addr, pnn) == 0) {
1857 move/failover an ip address to a specific node
1859 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1862 ctdb_sock_addr addr;
1864 assert_single_node_only();
1871 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1872 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1877 if (sscanf(argv[1], "%u", &pnn) != 1) {
1878 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1882 if (!try_moveip(ctdb, &addr, pnn)) {
1883 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1890 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1894 data.dptr = (uint8_t *)&pnn;
1895 data.dsize = sizeof(uint32_t);
1896 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1898 ("Failed to send message to force node %u to be a rebalancing target\n",
1908 rebalance a node by setting it to allow failback and triggering a
1911 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1913 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1918 assert_single_node_only();
1924 /* Determine the nodes where IPs need to be reloaded */
1925 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1926 options.pnn, true, &nodes, &pnn_mode)) {
1931 for (i = 0; i < talloc_array_length(nodes); i++) {
1932 if (!rebalance_node(ctdb, nodes[i])) {
1938 talloc_free(tmp_ctx);
1942 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1944 struct ctdb_public_ip ip;
1947 uint32_t disable_time;
1949 struct ctdb_node_map *nodemap=NULL;
1950 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1953 data.dptr = (uint8_t*)&disable_time;
1954 data.dsize = sizeof(disable_time);
1955 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1957 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1964 data.dptr = (uint8_t *)&ip;
1965 data.dsize = sizeof(ip);
1967 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1969 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1970 talloc_free(tmp_ctx);
1974 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1975 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1982 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1983 talloc_free(tmp_ctx);
1987 talloc_free(tmp_ctx);
1992 release an ip form all nodes and have it re-assigned by recd
1994 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1996 ctdb_sock_addr addr;
1998 assert_single_node_only();
2005 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2006 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2010 if (rebalance_ip(ctdb, &addr) != 0) {
2011 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2018 static int getips_store_callback(void *param, void *data)
2020 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2021 struct ctdb_all_public_ips *ips = param;
2025 ips->ips[i].pnn = node_ip->pnn;
2026 ips->ips[i].addr = node_ip->addr;
2030 static int getips_count_callback(void *param, void *data)
2032 uint32_t *count = param;
2039 static uint32_t *ip_key(ctdb_sock_addr *ip)
2041 static uint32_t key[IP_KEYLEN];
2043 bzero(key, sizeof(key));
2045 switch (ip->sa.sa_family) {
2047 key[0] = ip->ip.sin_addr.s_addr;
2050 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2058 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2065 static void *add_ip_callback(void *parm, void *data)
2071 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2073 struct ctdb_all_public_ips *tmp_ips;
2074 struct ctdb_node_map *nodemap=NULL;
2075 trbt_tree_t *ip_tree;
2079 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2081 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2085 ip_tree = trbt_create(tmp_ctx, 0);
2087 for(i=0;i<nodemap->num;i++){
2088 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2091 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2095 /* read the public ip list from this node */
2096 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2098 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2102 for (j=0; j<tmp_ips->num;j++) {
2103 struct ctdb_public_ip *node_ip;
2105 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2106 node_ip->pnn = tmp_ips->ips[j].pnn;
2107 node_ip->addr = tmp_ips->ips[j].addr;
2109 trbt_insertarray32_callback(ip_tree,
2110 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2114 talloc_free(tmp_ips);
2119 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2121 len = offsetof(struct ctdb_all_public_ips, ips) +
2122 count*sizeof(struct ctdb_public_ip);
2123 tmp_ips = talloc_zero_size(tmp_ctx, len);
2124 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2132 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2134 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2136 event_add_timed(ctdb->ev, ctdb,
2137 timeval_current_ofs(1, 0),
2138 ctdb_every_second, ctdb);
2141 struct srvid_reply_handler_data {
2145 const char *srvid_str;
2148 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2153 struct srvid_reply_handler_data *d =
2154 (struct srvid_reply_handler_data *)private_data;
2158 if (data.dsize != sizeof(ret)) {
2159 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2163 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2164 ret = *(int32_t *)data.dptr;
2167 ("%s failed with result %d\n", d->srvid_str, ret));
2171 if (!d->wait_for_all) {
2176 /* Wait for all replies */
2178 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2179 if (d->nodes[i] == ret) {
2181 ("%s reply received from node %u\n",
2182 d->srvid_str, ret));
2185 if (d->nodes[i] != -1) {
2186 /* Found a node that hasn't yet replied */
2192 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2193 * or replies from all connected nodes. arg is the data argument to
2194 * pass in the srvid_request structure - pass 0 if this isn't needed.
2196 static int srvid_broadcast(struct ctdb_context *ctdb,
2197 uint64_t srvid, uint32_t *arg,
2198 const char *srvid_str, bool wait_for_all)
2203 uint64_t reply_srvid;
2204 struct srvid_request request;
2205 struct srvid_request_data request_data;
2206 struct srvid_reply_handler_data reply_data;
2209 ZERO_STRUCT(request);
2211 /* Time ticks to enable timeouts to be processed */
2212 event_add_timed(ctdb->ev, ctdb,
2213 timeval_current_ofs(1, 0),
2214 ctdb_every_second, ctdb);
2216 pnn = ctdb_get_pnn(ctdb);
2217 reply_srvid = getpid();
2221 request.srvid = reply_srvid;
2223 data.dptr = (uint8_t *)&request;
2224 data.dsize = sizeof(request);
2226 request_data.pnn = pnn;
2227 request_data.srvid = reply_srvid;
2228 request_data.data = *arg;
2230 data.dptr = (uint8_t *)&request_data;
2231 data.dsize = sizeof(request_data);
2234 /* Register message port for reply from recovery master */
2235 ctdb_client_set_message_handler(ctdb, reply_srvid,
2236 srvid_broadcast_reply_handler,
2239 reply_data.wait_for_all = wait_for_all;
2240 reply_data.nodes = NULL;
2241 reply_data.srvid_str = srvid_str;
2244 reply_data.done = false;
2247 struct ctdb_node_map *nodemap;
2249 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2250 CTDB_CURRENT_NODE, ctdb, &nodemap);
2253 ("Unable to get nodemap from current node, try again\n"));
2258 if (reply_data.nodes != NULL) {
2259 talloc_free(reply_data.nodes);
2261 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2264 talloc_free(nodemap);
2267 /* Send to all connected nodes. Only recmaster replies */
2268 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2271 /* This can only happen if the socket is closed and
2272 * there's no way to recover from that, so don't try
2276 ("Failed to send %s request to connected nodes\n",
2281 tv = timeval_current();
2282 /* This loop terminates the reply is received */
2283 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2284 event_loop_once(ctdb->ev);
2287 if (!reply_data.done) {
2289 ("Still waiting for confirmation of %s\n", srvid_str));
2294 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2296 talloc_free(reply_data.nodes);
2301 static int ipreallocate(struct ctdb_context *ctdb)
2303 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2304 "IP reallocation", false);
2308 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2310 return ipreallocate(ctdb);
2314 add a public ip address to a node
2316 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2319 int len, retries = 0;
2321 ctdb_sock_addr addr;
2322 struct ctdb_control_ip_iface *pub;
2323 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2324 struct ctdb_all_public_ips *ips;
2328 talloc_free(tmp_ctx);
2332 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2333 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2334 talloc_free(tmp_ctx);
2338 /* read the public ip list from the node */
2339 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2341 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2342 talloc_free(tmp_ctx);
2345 for (i=0;i<ips->num;i++) {
2346 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2347 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2354 /* Dont timeout. This command waits for an ip reallocation
2355 which sometimes can take wuite a while if there has
2356 been a recent recovery
2360 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2361 pub = talloc_size(tmp_ctx, len);
2362 CTDB_NO_MEMORY(ctdb, pub);
2366 pub->len = strlen(argv[1])+1;
2367 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2370 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2372 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2376 } while (retries < 5 && ret != 0);
2378 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2379 talloc_free(tmp_ctx);
2383 if (rebalance_node(ctdb, options.pnn) != 0) {
2384 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2388 talloc_free(tmp_ctx);
2393 add a public ip address to a node
2395 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2397 ctdb_sock_addr addr;
2403 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2404 printf("Badly formed ip : %s\n", argv[0]);
2408 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2413 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2415 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2417 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2418 struct ctdb_node_map *nodemap=NULL;
2419 struct ctdb_all_public_ips *ips;
2422 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2424 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2428 /* remove it from the nodes that are not hosting the ip currently */
2429 for(i=0;i<nodemap->num;i++){
2430 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2433 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2435 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2439 for (j=0;j<ips->num;j++) {
2440 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2448 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2452 options.pnn = nodemap->nodes[i].pnn;
2453 control_delip(ctdb, argc, argv);
2457 /* remove it from every node (also the one hosting it) */
2458 for(i=0;i<nodemap->num;i++){
2459 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2462 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2464 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2468 for (j=0;j<ips->num;j++) {
2469 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2477 options.pnn = nodemap->nodes[i].pnn;
2478 control_delip(ctdb, argc, argv);
2481 talloc_free(tmp_ctx);
2486 delete a public ip address from a node
2488 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2491 ctdb_sock_addr addr;
2492 struct ctdb_control_ip_iface pub;
2493 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2494 struct ctdb_all_public_ips *ips;
2497 talloc_free(tmp_ctx);
2501 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2502 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2506 if (options.pnn == CTDB_BROADCAST_ALL) {
2507 return control_delip_all(ctdb, argc, argv, &addr);
2514 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2516 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2517 talloc_free(tmp_ctx);
2521 for (i=0;i<ips->num;i++) {
2522 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2528 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2529 ctdb_addr_to_str(&addr)));
2530 talloc_free(tmp_ctx);
2534 /* This is an optimisation. If this node is hosting the IP
2535 * then try to move it somewhere else without invoking a full
2536 * takeover run. We don't care if this doesn't work!
2538 if (ips->ips[i].pnn == options.pnn) {
2539 (void) try_moveip(ctdb, &addr, -1);
2542 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2544 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2545 talloc_free(tmp_ctx);
2549 talloc_free(tmp_ctx);
2553 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2554 int argc, const char **argv)
2556 struct ctdb_control_killtcp *killtcp;
2557 int max_entries, current, i;
2558 struct timeval timeout;
2559 char line[128], src[128], dst[128];
2562 struct client_async_data *async_data;
2563 struct ctdb_client_control_state *state;
2573 while (!feof(stdin)) {
2574 if (fgets(line, sizeof(line), stdin) == NULL) {
2578 /* Silently skip empty lines */
2579 if (line[0] == '\n') {
2583 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2584 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2586 talloc_free(killtcp);
2590 if (current >= max_entries) {
2591 max_entries += 1024;
2592 killtcp = talloc_realloc(ctdb, killtcp,
2593 struct ctdb_control_killtcp,
2595 CTDB_NO_MEMORY(ctdb, killtcp);
2598 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2599 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2601 talloc_free(killtcp);
2605 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2606 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2608 talloc_free(killtcp);
2615 async_data = talloc_zero(ctdb, struct client_async_data);
2616 if (async_data == NULL) {
2617 talloc_free(killtcp);
2621 for (i = 0; i < current; i++) {
2623 data.dsize = sizeof(struct ctdb_control_killtcp);
2624 data.dptr = (unsigned char *)&killtcp[i];
2626 timeout = TIMELIMIT();
2627 state = ctdb_control_send(ctdb, options.pnn, 0,
2628 CTDB_CONTROL_KILL_TCP, 0, data,
2629 async_data, &timeout, NULL);
2631 if (state == NULL) {
2633 ("Failed to call async killtcp control to node %u\n",
2635 talloc_free(killtcp);
2639 ctdb_client_async_add(async_data, state);
2642 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2643 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2644 talloc_free(killtcp);
2648 talloc_free(killtcp);
2654 kill a tcp connection
2656 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2659 struct ctdb_control_killtcp killtcp;
2661 assert_single_node_only();
2664 return kill_tcp_from_file(ctdb, argc, argv);
2671 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2672 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2676 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2677 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2681 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2683 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2694 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2697 ctdb_sock_addr addr;
2699 assert_single_node_only();
2705 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2706 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2710 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2712 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2720 register a server id
2722 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2725 struct ctdb_server_id server_id;
2731 server_id.pnn = strtoul(argv[0], NULL, 0);
2732 server_id.type = strtoul(argv[1], NULL, 0);
2733 server_id.server_id = strtoul(argv[2], NULL, 0);
2735 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2737 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2740 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2746 unregister a server id
2748 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2751 struct ctdb_server_id server_id;
2757 server_id.pnn = strtoul(argv[0], NULL, 0);
2758 server_id.type = strtoul(argv[1], NULL, 0);
2759 server_id.server_id = strtoul(argv[2], NULL, 0);
2761 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2763 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2770 check if a server id exists
2772 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2776 struct ctdb_server_id server_id;
2782 server_id.pnn = strtoul(argv[0], NULL, 0);
2783 server_id.type = strtoul(argv[1], NULL, 0);
2784 server_id.server_id = strtoul(argv[2], NULL, 0);
2786 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2788 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2793 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2795 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2801 get a list of all server ids that are registered on a node
2803 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2806 struct ctdb_server_id_list *server_ids;
2808 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2810 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2814 for (i=0; i<server_ids->num; i++) {
2815 printf("Server id %d:%d:%d\n",
2816 server_ids->server_ids[i].pnn,
2817 server_ids->server_ids[i].type,
2818 server_ids->server_ids[i].server_id);
2825 check if a server id exists
2827 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2829 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2835 talloc_free(tmp_ctx);
2839 ids = talloc_array(tmp_ctx, uint64_t, argc);
2840 result = talloc_array(tmp_ctx, uint8_t, argc);
2842 for (i = 0; i < argc; i++) {
2843 ids[i] = strtoull(argv[i], NULL, 0);
2846 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2847 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2849 talloc_free(tmp_ctx);
2853 for (i=0; i < argc; i++) {
2854 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2855 result[i] ? "exists" : "does not exist");
2858 talloc_free(tmp_ctx);
2863 send a tcp tickle ack
2865 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2868 ctdb_sock_addr src, dst;
2874 if (!parse_ip_port(argv[0], &src)) {
2875 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2879 if (!parse_ip_port(argv[1], &dst)) {
2880 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2884 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2888 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2895 display public ip status
2897 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2900 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2901 struct ctdb_all_public_ips *ips;
2903 if (options.pnn == CTDB_BROADCAST_ALL) {
2904 /* read the list of public ips from all nodes */
2905 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2907 /* read the public ip list from this node */
2908 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2911 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2912 talloc_free(tmp_ctx);
2916 if (options.machinereadable){
2917 printf(":Public IP:Node:");
2918 if (options.verbose){
2919 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2923 if (options.pnn == CTDB_BROADCAST_ALL) {
2924 printf("Public IPs on ALL nodes\n");
2926 printf("Public IPs on node %u\n", options.pnn);
2930 for (i=1;i<=ips->num;i++) {
2931 struct ctdb_control_public_ip_info *info = NULL;
2933 char *aciface = NULL;
2934 char *avifaces = NULL;
2935 char *cifaces = NULL;
2937 if (options.pnn == CTDB_BROADCAST_ALL) {
2938 pnn = ips->ips[ips->num-i].pnn;
2944 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2945 &ips->ips[ips->num-i].addr, &info);
2952 for (j=0; j < info->num; j++) {
2953 if (cifaces == NULL) {
2954 cifaces = talloc_strdup(info,
2955 info->ifaces[j].name);
2957 cifaces = talloc_asprintf_append(cifaces,
2959 info->ifaces[j].name);
2962 if (info->active_idx == j) {
2963 aciface = info->ifaces[j].name;
2966 if (info->ifaces[j].link_state == 0) {
2970 if (avifaces == NULL) {
2971 avifaces = talloc_strdup(info, info->ifaces[j].name);
2973 avifaces = talloc_asprintf_append(avifaces,
2975 info->ifaces[j].name);
2980 if (options.machinereadable){
2982 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2983 ips->ips[ips->num-i].pnn);
2984 if (options.verbose){
2987 avifaces?avifaces:"",
2988 cifaces?cifaces:"");
2992 if (options.verbose) {
2993 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2994 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2995 ips->ips[ips->num-i].pnn,
2997 avifaces?avifaces:"",
2998 cifaces?cifaces:"");
3001 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3002 ips->ips[ips->num-i].pnn);
3008 talloc_free(tmp_ctx);
3015 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3018 ctdb_sock_addr addr;
3019 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3020 struct ctdb_control_public_ip_info *info;
3023 talloc_free(tmp_ctx);
3027 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3028 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3032 /* read the public ip info from this node */
3033 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3034 tmp_ctx, &addr, &info);
3036 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3037 argv[0], options.pnn));
3038 talloc_free(tmp_ctx);
3042 printf("Public IP[%s] info on node %u\n",
3043 ctdb_addr_to_str(&info->ip.addr),
3046 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3047 ctdb_addr_to_str(&info->ip.addr),
3048 info->ip.pnn, info->num);
3050 for (i=0; i<info->num; i++) {
3051 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3053 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3054 i+1, info->ifaces[i].name,
3055 info->ifaces[i].link_state?"up":"down",
3056 (unsigned int)info->ifaces[i].references,
3057 (i==info->active_idx)?" (active)":"");
3060 talloc_free(tmp_ctx);
3065 display interfaces status
3067 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3069 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3071 struct ctdb_control_get_ifaces *ifaces;
3074 /* read the public ip list from this node */
3075 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3077 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3079 talloc_free(tmp_ctx);
3083 if (options.machinereadable){
3084 printf(":Name:LinkStatus:References:\n");
3086 printf("Interfaces on node %u\n", options.pnn);
3089 for (i=0; i<ifaces->num; i++) {
3090 if (options.machinereadable){
3091 printf(":%s:%s:%u\n",
3092 ifaces->ifaces[i].name,
3093 ifaces->ifaces[i].link_state?"1":"0",
3094 (unsigned int)ifaces->ifaces[i].references);
3096 printf("name:%s link:%s references:%u\n",
3097 ifaces->ifaces[i].name,
3098 ifaces->ifaces[i].link_state?"up":"down",
3099 (unsigned int)ifaces->ifaces[i].references);
3103 talloc_free(tmp_ctx);
3109 set link status of an interface
3111 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3114 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3115 struct ctdb_control_iface_info info;
3123 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3124 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3126 talloc_free(tmp_ctx);
3129 strcpy(info.name, argv[0]);
3131 if (strcmp(argv[1], "up") == 0) {
3132 info.link_state = 1;
3133 } else if (strcmp(argv[1], "down") == 0) {
3134 info.link_state = 0;
3136 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3138 talloc_free(tmp_ctx);
3142 /* read the public ip list from this node */
3143 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3146 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3147 argv[0], options.pnn));
3148 talloc_free(tmp_ctx);
3152 talloc_free(tmp_ctx);
3157 display pid of a ctdb daemon
3159 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3164 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3166 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3169 printf("Pid:%d\n", pid);
3174 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3176 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3178 update_flags_handler_t handler,
3183 struct ctdb_node_map *nodemap = NULL;
3187 /* Check if the node is already in the desired state */
3188 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3190 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3193 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3194 if (set_flag == flag_is_set) {
3195 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3196 (set_flag ? "already" : "not"), desc));
3201 if (!handler(ctdb, data)) {
3202 DEBUG(DEBUG_WARNING,
3203 ("Failed to send control to set state %s on node %u, try again\n",
3204 desc, options.pnn));
3209 /* Read the nodemap and verify the change took effect.
3210 * Even if the above control/hanlder timed out then it
3211 * could still have worked!
3213 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3216 DEBUG(DEBUG_WARNING,
3217 ("Unable to get nodemap from local node, try again\n"));
3219 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3220 } while (nodemap == NULL || (set_flag != flag_is_set));
3222 return ipreallocate(ctdb);
3225 /* Administratively disable a node */
3226 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3230 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3231 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3235 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3237 return update_flags_and_ipreallocate(ctdb, NULL,
3238 update_flags_disabled,
3239 NODE_FLAGS_PERMANENTLY_DISABLED,
3241 true /* set_flag*/);
3244 /* Administratively re-enable a node */
3245 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3249 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3250 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3254 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3256 return update_flags_and_ipreallocate(ctdb, NULL,
3257 update_flags_not_disabled,
3258 NODE_FLAGS_PERMANENTLY_DISABLED,
3260 false /* set_flag*/);
3264 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3268 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3273 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3275 return update_flags_and_ipreallocate(ctdb, NULL,
3276 update_flags_stopped,
3279 true /* set_flag*/);
3282 /* Continue a stopped node */
3283 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3287 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3292 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3294 return update_flags_and_ipreallocate(ctdb, NULL,
3295 update_flags_not_stopped,
3298 false /* set_flag */);
3301 static uint32_t get_generation(struct ctdb_context *ctdb)
3303 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3304 struct ctdb_vnn_map *vnnmap=NULL;
3306 uint32_t generation;
3308 /* wait until the recmaster is not in recovery mode */
3310 uint32_t recmode, recmaster;
3312 if (vnnmap != NULL) {
3313 talloc_free(vnnmap);
3317 /* get the recmaster */
3318 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3320 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3321 talloc_free(tmp_ctx);
3325 /* get recovery mode */
3326 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3328 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3329 talloc_free(tmp_ctx);
3333 /* get the current generation number */
3334 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3336 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3337 talloc_free(tmp_ctx);
3341 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3342 generation = vnnmap->generation;
3343 talloc_free(tmp_ctx);
3351 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3353 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3356 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3361 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3363 struct ctdb_ban_time bantime;
3369 bantime.pnn = options.pnn;
3370 bantime.time = strtoul(argv[0], NULL, 0);
3372 if (bantime.time == 0) {
3373 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3377 return update_flags_and_ipreallocate(ctdb, &bantime,
3378 update_state_banned,
3381 true /* set_flag*/);
3386 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3388 struct ctdb_ban_time bantime;
3390 bantime.pnn = options.pnn;
3393 return update_flags_and_ipreallocate(ctdb, &bantime,
3394 update_state_banned,
3397 false /* set_flag*/);
3401 show ban information for a node
3403 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3406 struct ctdb_node_map *nodemap=NULL;
3407 struct ctdb_ban_time *bantime;
3409 /* verify the node exists */
3410 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3412 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3416 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3418 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3422 if (bantime->time == 0) {
3423 printf("Node %u is not banned\n", bantime->pnn);
3425 printf("Node %u is banned, %d seconds remaining\n",
3426 bantime->pnn, bantime->time);
3435 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3439 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3441 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3451 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3454 uint32_t generation, next_generation;
3457 /* "force" option ignores freeze failure and forces recovery */
3458 force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3460 /* record the current generation number */
3461 generation = get_generation(ctdb);
3463 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3466 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3469 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3472 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3474 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3478 /* wait until we are in a new generation */
3480 next_generation = get_generation(ctdb);
3481 if (next_generation != generation) {
3492 display monitoring mode of a remote node
3494 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3499 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3501 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3504 if (!options.machinereadable){
3505 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3508 printf(":%d:\n",monmode);
3515 display capabilities of a remote node
3517 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3519 uint32_t capabilities;
3522 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3524 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3528 if (!options.machinereadable){
3529 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3530 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3531 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3532 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3534 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3535 printf(":%d:%d:%d:%d:\n",
3536 !!(capabilities&CTDB_CAP_RECMASTER),
3537 !!(capabilities&CTDB_CAP_LMASTER),
3538 !!(capabilities&CTDB_CAP_LVS),
3539 !!(capabilities&CTDB_CAP_NATGW));
3545 display lvs configuration
3548 static uint32_t lvs_exclude_flags[] = {
3549 /* Look for a nice healthy node */
3550 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3551 /* If not found, an UNHEALTHY node will do */
3552 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3556 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3558 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3559 struct ctdb_node_map *orig_nodemap=NULL;
3560 struct ctdb_node_map *nodemap;
3563 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3564 tmp_ctx, &orig_nodemap);
3566 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3567 talloc_free(tmp_ctx);
3571 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3573 if (nodemap == NULL) {
3581 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3582 struct ctdb_node_map *t =
3583 filter_nodemap_by_flags(ctdb, nodemap,
3584 lvs_exclude_flags[i]);
3591 /* At least 1 node without excluded flags */
3593 for (j = 0; j < t->num; j++) {
3594 printf("%d:%s\n", t->nodes[j].pnn,
3595 ctdb_addr_to_str(&t->nodes[j].addr));
3602 talloc_free(tmp_ctx);
3607 display who is the lvs master
3609 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3611 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3612 struct ctdb_node_map *orig_nodemap=NULL;
3613 struct ctdb_node_map *nodemap;
3616 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3617 tmp_ctx, &orig_nodemap);
3619 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3620 talloc_free(tmp_ctx);
3624 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3626 if (nodemap == NULL) {
3632 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3633 struct ctdb_node_map *t =
3634 filter_nodemap_by_flags(ctdb, nodemap,
3635 lvs_exclude_flags[i]);
3643 printf(options.machinereadable ?
3644 "%d\n" : "Node %d is LVS master\n",
3651 printf("There is no LVS master\n");
3654 talloc_free(tmp_ctx);
3659 disable monitoring on a node
3661 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3666 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3668 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3671 printf("Monitoring mode:%s\n","DISABLED");
3677 enable monitoring on a node
3679 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3684 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3686 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3689 printf("Monitoring mode:%s\n","ACTIVE");
3695 display remote list of keys/data for a db
3697 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3699 const char *db_name;
3700 struct ctdb_db_context *ctdb_db;
3702 struct ctdb_dump_db_context c;
3709 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3713 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3714 if (ctdb_db == NULL) {
3715 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3719 if (options.printlmaster) {
3720 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3721 ctdb, &ctdb->vnn_map);
3723 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3731 c.printemptyrecords = (bool)options.printemptyrecords;
3732 c.printdatasize = (bool)options.printdatasize;
3733 c.printlmaster = (bool)options.printlmaster;
3734 c.printhash = (bool)options.printhash;
3735 c.printrecordflags = (bool)options.printrecordflags;
3737 /* traverse and dump the cluster tdb */
3738 ret = ctdb_dump_db(ctdb_db, &c);
3740 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3741 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3742 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3746 talloc_free(ctdb_db);
3748 printf("Dumped %d records\n", ret);
3752 struct cattdb_data {
3753 struct ctdb_context *ctdb;
3757 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3759 struct cattdb_data *d = private_data;
3760 struct ctdb_dump_db_context c;
3766 c.printemptyrecords = (bool)options.printemptyrecords;
3767 c.printdatasize = (bool)options.printdatasize;
3768 c.printlmaster = false;
3769 c.printhash = (bool)options.printhash;
3770 c.printrecordflags = true;
3772 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3776 cat the local tdb database using same format as catdb
3778 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3780 const char *db_name;
3781 struct ctdb_db_context *ctdb_db;
3782 struct cattdb_data d;
3789 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3793 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3794 if (ctdb_db == NULL) {
3795 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3799 /* traverse the local tdb */
3802 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3803 printf("Failed to cattdb data\n");
3806 talloc_free(ctdb_db);
3808 printf("Dumped %d records\n", d.count);
3813 display the content of a database key
3815 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3817 const char *db_name;
3818 struct ctdb_db_context *ctdb_db;
3819 struct ctdb_record_handle *h;
3820 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3828 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3832 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3833 if (ctdb_db == NULL) {
3834 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3838 key.dptr = discard_const(argv[1]);
3839 key.dsize = strlen((char *)key.dptr);
3841 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3843 printf("Failed to fetch record '%s' on node %d\n",
3844 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3845 talloc_free(tmp_ctx);
3849 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3851 talloc_free(tmp_ctx);
3852 talloc_free(ctdb_db);
3857 display the content of a database key
3859 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3861 const char *db_name;
3862 struct ctdb_db_context *ctdb_db;
3863 struct ctdb_record_handle *h;
3864 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3872 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3876 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3877 if (ctdb_db == NULL) {
3878 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3882 key.dptr = discard_const(argv[1]);
3883 key.dsize = strlen((char *)key.dptr);
3885 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3887 printf("Failed to fetch record '%s' on node %d\n",
3888 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3889 talloc_free(tmp_ctx);
3893 data.dptr = discard_const(argv[2]);
3894 data.dsize = strlen((char *)data.dptr);
3896 if (ctdb_record_store(h, data) != 0) {
3897 printf("Failed to store record\n");
3901 talloc_free(tmp_ctx);
3902 talloc_free(ctdb_db);
3907 fetch a record from a persistent database
3909 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3911 const char *db_name;
3912 struct ctdb_db_context *ctdb_db;
3913 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3914 struct ctdb_transaction_handle *h;
3921 talloc_free(tmp_ctx);
3925 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3926 talloc_free(tmp_ctx);
3930 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3932 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3933 talloc_free(tmp_ctx);
3937 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3938 if (ctdb_db == NULL) {
3939 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3940 talloc_free(tmp_ctx);
3944 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3946 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3947 talloc_free(tmp_ctx);
3951 key.dptr = discard_const(argv[1]);
3952 key.dsize = strlen(argv[1]);
3953 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3955 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3956 talloc_free(tmp_ctx);
3960 if (data.dsize == 0 || data.dptr == NULL) {
3961 DEBUG(DEBUG_ERR,("Record is empty\n"));
3962 talloc_free(tmp_ctx);
3967 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3969 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3970 talloc_free(tmp_ctx);
3973 write(fd, data.dptr, data.dsize);
3976 write(1, data.dptr, data.dsize);
3979 /* abort the transaction */
3983 talloc_free(tmp_ctx);
3988 fetch a record from a tdb-file
3990 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3992 const char *tdb_file;
3995 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4004 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4006 printf("Failed to open TDB file %s\n", tdb_file);
4010 if (!strncmp(argv[1], "0x", 2)) {
4011 key = hextodata(tmp_ctx, argv[1] + 2);
4012 if (key.dsize == 0) {
4013 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4017 key.dptr = discard_const(argv[1]);
4018 key.dsize = strlen(argv[1]);
4021 data = tdb_fetch(tdb, key);
4022 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4023 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4031 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4033 printf("Failed to open output file %s\n", argv[2]);
4036 if (options.verbose){
4037 write(fd, data.dptr, data.dsize);
4039 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4043 if (options.verbose){
4044 write(1, data.dptr, data.dsize);
4046 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4050 talloc_free(tmp_ctx);
4055 store a record and header to a tdb-file
4057 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4059 const char *tdb_file;
4061 TDB_DATA key, value, data;
4062 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4063 struct ctdb_ltdb_header header;
4071 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4073 printf("Failed to open TDB file %s\n", tdb_file);
4077 if (!strncmp(argv[1], "0x", 2)) {
4078 key = hextodata(tmp_ctx, argv[1] + 2);
4079 if (key.dsize == 0) {
4080 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4084 key.dptr = discard_const(argv[1]);
4085 key.dsize = strlen(argv[1]);
4088 if (!strncmp(argv[2], "0x", 2)) {
4089 value = hextodata(tmp_ctx, argv[2] + 2);
4090 if (value.dsize == 0) {
4091 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4095 value.dptr = discard_const(argv[2]);
4096 value.dsize = strlen(argv[2]);
4099 ZERO_STRUCT(header);
4101 header.rsn = atoll(argv[3]);
4104 header.dmaster = atoi(argv[4]);
4107 header.flags = atoi(argv[5]);
4110 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4111 data.dptr = talloc_size(tmp_ctx, data.dsize);
4112 if (data.dptr == NULL) {
4113 printf("Failed to allocate header+value\n");
4117 *(struct ctdb_ltdb_header *)data.dptr = header;
4118 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4120 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4121 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4128 talloc_free(tmp_ctx);
4133 write a record to a persistent database
4135 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4137 const char *db_name;
4138 struct ctdb_db_context *ctdb_db;
4139 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4140 struct ctdb_transaction_handle *h;
4146 talloc_free(tmp_ctx);
4150 fd = open(argv[2], O_RDONLY);
4152 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4153 talloc_free(tmp_ctx);
4157 ret = fstat(fd, &st);
4159 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4161 talloc_free(tmp_ctx);
4165 if (!S_ISREG(st.st_mode)) {
4166 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4168 talloc_free(tmp_ctx);
4172 data.dsize = st.st_size;
4173 if (data.dsize == 0) {
4176 data.dptr = talloc_size(tmp_ctx, data.dsize);
4177 if (data.dptr == NULL) {
4178 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4180 talloc_free(tmp_ctx);
4183 ret = read(fd, data.dptr, data.dsize);
4184 if (ret != data.dsize) {
4185 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4187 talloc_free(tmp_ctx);
4196 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4197 if (ctdb_db == NULL) {
4198 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4199 talloc_free(tmp_ctx);
4203 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4205 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4206 talloc_free(tmp_ctx);
4210 key.dptr = discard_const(argv[1]);
4211 key.dsize = strlen(argv[1]);
4212 ret = ctdb_transaction_store(h, key, data);
4214 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4215 talloc_free(tmp_ctx);
4219 ret = ctdb_transaction_commit(h);
4221 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4222 talloc_free(tmp_ctx);
4227 talloc_free(tmp_ctx);
4232 * delete a record from a persistent database
4234 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4236 const char *db_name;
4237 struct ctdb_db_context *ctdb_db;
4238 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4239 struct ctdb_transaction_handle *h;
4246 talloc_free(tmp_ctx);
4250 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4251 talloc_free(tmp_ctx);
4255 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4257 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4258 talloc_free(tmp_ctx);
4262 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4263 if (ctdb_db == NULL) {
4264 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4265 talloc_free(tmp_ctx);
4269 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4271 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4272 talloc_free(tmp_ctx);
4276 key.dptr = discard_const(argv[1]);
4277 key.dsize = strlen(argv[1]);
4278 ret = ctdb_transaction_store(h, key, tdb_null);
4280 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4281 talloc_free(tmp_ctx);
4285 ret = ctdb_transaction_commit(h);
4287 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4288 talloc_free(tmp_ctx);
4292 talloc_free(tmp_ctx);
4296 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4301 const char *ret; /* Next byte after successfully parsed value */
4303 /* Error, unless someone says otherwise */
4305 /* Indicates no value to parse */
4308 /* Skip whitespace */
4309 n = strspn(s, " \t");
4313 /* Quoted ASCII string - no wide characters! */
4315 n = strcspn(t, "\"");
4319 data->dptr = talloc_memdup(mem_ctx, t, n);
4320 CTDB_NOMEM_ABORT(data->dptr);
4324 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4327 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4333 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4334 TDB_DATA *key, TDB_DATA *value)
4336 char line [1024]; /* FIXME: make this more flexible? */
4340 ptr = fgets(line, sizeof(line), file);
4347 t = ptrans_parse_string(mem_ctx, line, key);
4348 if (t == NULL || key->dptr == NULL) {
4349 /* Line Ignored but not EOF */
4354 t = ptrans_parse_string(mem_ctx, t, value);
4356 /* Line Ignored but not EOF */
4357 talloc_free(key->dptr);
4366 * Update a persistent database as per file/stdin
4368 static int control_ptrans(struct ctdb_context *ctdb,
4369 int argc, const char **argv)
4371 const char *db_name;
4372 struct ctdb_db_context *ctdb_db;
4373 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4374 struct ctdb_transaction_handle *h;
4375 TDB_DATA key, value;
4380 talloc_free(tmp_ctx);
4386 file = fopen(argv[1], "r");
4388 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4389 talloc_free(tmp_ctx);
4396 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4397 if (ctdb_db == NULL) {
4398 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4402 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4404 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4408 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4409 if (key.dsize != 0) {
4410 ret = ctdb_transaction_store(h, key, value);
4411 /* Minimise memory use */
4412 talloc_free(key.dptr);
4413 if (value.dptr != NULL) {
4414 talloc_free(value.dptr);
4417 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4418 ctdb_transaction_cancel(h);
4424 ret = ctdb_transaction_commit(h);
4426 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4430 if (file != stdin) {
4433 talloc_free(tmp_ctx);
4437 if (file != stdin) {
4441 talloc_free(tmp_ctx);
4446 check if a service is bound to a port or not
4448 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4453 struct sockaddr_in sin;
4456 printf("Use: ctdb chktcport <port>\n");
4460 port = atoi(argv[0]);
4462 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4464 printf("Failed to open local socket\n");
4468 v = fcntl(s, F_GETFL, 0);
4469 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4470 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4473 bzero(&sin, sizeof(sin));
4474 sin.sin_family = PF_INET;
4475 sin.sin_port = htons(port);
4476 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4479 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4488 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4489 TDB_DATA data, void *private_data)
4491 DEBUG(DEBUG_ERR,("Log data received\n"));
4492 if (data.dsize > 0) {
4493 printf("%s", data.dptr);
4500 display a list of log messages from the in memory ringbuffer
4502 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4506 struct ctdb_get_log_addr log_addr;
4510 /* Process options */
4512 log_addr.pnn = ctdb_get_pnn(ctdb);
4513 log_addr.level = DEBUG_NOTICE;
4514 for (i = 0; i < argc; i++) {
4515 if (strcmp(argv[i], "recoverd") == 0) {
4516 main_daemon = false;
4518 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4519 log_addr.level = get_debug_by_desc(argv[i]);
4521 log_addr.level = strtol(argv[i], NULL, 0);
4526 /* Our message port is our PID */
4527 log_addr.srvid = getpid();
4529 data.dptr = (unsigned char *)&log_addr;
4530 data.dsize = sizeof(log_addr);
4532 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4534 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4537 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4542 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4544 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4545 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4546 if (ret != 0 || res != 0) {
4547 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4548 talloc_free(tmp_ctx);
4551 talloc_free(tmp_ctx);
4553 ret = ctdb_client_send_message(ctdb, options.pnn,
4554 CTDB_SRVID_GETLOG, data);
4556 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4561 tv = timeval_current();
4562 /* this loop will terminate when we have received the reply */
4563 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4564 event_loop_once(ctdb->ev);
4567 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4573 clear the in memory log area
4575 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4579 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4580 /* "recoverd" not given - get logs from main daemon */
4583 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4585 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4586 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4587 if (ret != 0 || res != 0) {
4588 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4589 talloc_free(tmp_ctx);
4593 talloc_free(tmp_ctx);
4595 TDB_DATA data; /* unused in recoverd... */
4598 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4600 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4608 /* Reload public IPs on a specified nodes */
4609 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4611 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4617 assert_single_node_only();
4623 /* Determine the nodes where IPs need to be reloaded */
4624 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4625 options.pnn, true, &nodes, &pnn_mode)) {
4631 /* Disable takeover runs on all connected nodes. A reply
4632 * indicating success is needed from each node so all nodes
4633 * will need to be active. This will retry until maxruntime
4634 * is exceeded, hence no error handling.
4636 * A check could be added to not allow reloading of IPs when
4637 * there are disconnected nodes. However, this should
4638 * probably be left up to the administrator.
4640 timeout = LONGTIMEOUT;
4641 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4642 "Disable takeover runs", true);
4644 /* Now tell all the desired nodes to reload their public IPs.
4645 * Keep trying this until it succeeds. This assumes all
4646 * failures are transient, which might not be true...
4648 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4649 nodes, 0, LONGTIMELIMIT(),
4651 NULL, NULL, NULL) != 0) {
4653 ("Unable to reload IPs on some nodes, try again.\n"));
4657 /* It isn't strictly necessary to wait until takeover runs are
4658 * re-enabled but doing so can't hurt.
4661 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4662 "Enable takeover runs", true);
4668 talloc_free(tmp_ctx);
4673 display a list of the databases on a remote ctdb
4675 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4678 struct ctdb_dbid_map *dbmap=NULL;
4680 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4682 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4686 if(options.machinereadable){
4687 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4688 for(i=0;i<dbmap->num;i++){
4696 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4697 dbmap->dbs[i].dbid, ctdb, &path);
4698 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4699 dbmap->dbs[i].dbid, ctdb, &name);
4700 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4701 dbmap->dbs[i].dbid, ctdb, &health);
4702 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4703 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4704 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4705 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4706 dbmap->dbs[i].dbid, name, path,
4707 !!(persistent), !!(sticky),
4708 !!(health), !!(readonly));
4713 printf("Number of databases:%d\n", dbmap->num);
4714 for(i=0;i<dbmap->num;i++){
4722 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4723 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4724 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4725 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4726 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4727 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4728 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4729 dbmap->dbs[i].dbid, name, path,
4730 persistent?" PERSISTENT":"",
4731 sticky?" STICKY":"",
4732 readonly?" READONLY":"",
4733 health?" UNHEALTHY":"");
4740 display the status of a database on a remote ctdb
4742 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4744 const char *db_name;
4754 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4758 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4759 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4760 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4761 db_id, db_name, path,
4762 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4763 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4764 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4765 (health ? health : "OK"));
4771 check if the local node is recmaster or not
4772 it will return 1 if this node is the recmaster and 0 if it is not
4773 or if the local ctdb daemon could not be contacted
4775 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4777 uint32_t mypnn, recmaster;
4780 assert_single_node_only();
4782 mypnn = getpnn(ctdb);
4784 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4786 printf("Failed to get the recmaster\n");
4790 if (recmaster != mypnn) {
4791 printf("this node is not the recmaster\n");
4795 printf("this node is the recmaster\n");
4802 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4805 struct timeval tv = timeval_current();
4806 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4808 printf("Unable to get ping response from node %u\n", options.pnn);
4811 printf("response from %u time=%.6f sec (%d clients)\n",
4812 options.pnn, timeval_elapsed(&tv), ret);
4819 get a node's runstate
4821 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4824 enum ctdb_runstate runstate;
4826 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4828 printf("Unable to get runstate response from node %u\n",
4833 enum ctdb_runstate t;
4835 for (i=0; i<argc; i++) {
4837 t = runstate_from_string(argv[i]);
4838 if (t == CTDB_RUNSTATE_UNKNOWN) {
4839 printf("Invalid run state (%s)\n", argv[i]);
4843 if (t == runstate) {
4850 printf("CTDB not in required run state (got %s)\n",
4851 runstate_to_string((enum ctdb_runstate)runstate));
4856 printf("%s\n", runstate_to_string(runstate));
4864 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4875 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4877 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4881 printf("%-23s = %u\n", name, value);
4888 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4899 value = strtoul(argv[1], NULL, 0);
4901 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4903 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4912 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4918 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4920 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4924 for (i=0;i<count;i++) {
4925 control_getvar(ctdb, 1, &list[i]);
4934 display debug level on a node
4936 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4941 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4943 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4946 if (options.machinereadable){
4947 printf(":Name:Level:\n");
4948 printf(":%s:%d:\n",get_debug_by_level(level),level);
4950 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4957 display reclock file of a node
4959 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4962 const char *reclock;
4964 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4966 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4969 if (options.machinereadable){
4970 if (reclock != NULL) {
4971 printf("%s", reclock);
4974 if (reclock == NULL) {
4975 printf("No reclock file used.\n");
4977 printf("Reclock file:%s\n", reclock);
4985 set the reclock file of a node
4987 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4990 const char *reclock;
4994 } else if (argc == 1) {
5000 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
5002 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
5009 set the natgw state on/off
5011 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
5014 uint32_t natgwstate;
5020 if (!strcmp(argv[0], "on")) {
5022 } else if (!strcmp(argv[0], "off")) {
5028 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
5030 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
5038 set the lmaster role on/off
5040 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5043 uint32_t lmasterrole;
5049 if (!strcmp(argv[0], "on")) {
5051 } else if (!strcmp(argv[0], "off")) {
5057 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
5059 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
5067 set the recmaster role on/off
5069 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5072 uint32_t recmasterrole;
5078 if (!strcmp(argv[0], "on")) {
5080 } else if (!strcmp(argv[0], "off")) {
5086 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5088 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5096 set debug level on a node or all nodes
5098 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5104 printf("You must specify the debug level. Valid levels are:\n");
5105 for (i=0; debug_levels[i].description != NULL; i++) {
5106 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5112 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
5113 level = get_debug_by_desc(argv[0]);
5115 level = strtol(argv[0], NULL, 0);
5118 for (i=0; debug_levels[i].description != NULL; i++) {
5119 if (level == debug_levels[i].level) {
5123 if (debug_levels[i].description == NULL) {
5124 printf("Invalid debug level, must be one of\n");
5125 for (i=0; debug_levels[i].description != NULL; i++) {
5126 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5131 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5133 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5142 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5148 priority = strtol(argv[0], NULL, 0);
5152 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5154 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5156 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5163 attach to a database
5165 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5167 const char *db_name;
5168 struct ctdb_db_context *ctdb_db;
5169 bool persistent = false;
5179 if (strcmp(argv[1], "persistent") != 0) {
5185 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5186 if (ctdb_db == NULL) {
5187 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5195 * detach from a database
5197 static int control_detach(struct ctdb_context *ctdb, int argc,
5202 int ret, i, status = 0;
5203 struct ctdb_node_map *nodemap = NULL;
5204 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5211 assert_single_node_only();
5213 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5216 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5217 "when recovery is active\n"));
5218 talloc_free(tmp_ctx);
5222 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5225 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5227 talloc_free(tmp_ctx);
5231 for (i=0; i<nodemap->num; i++) {
5234 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5238 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5242 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5243 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5244 "inactive (stopped or banned) node "
5245 "%u\n", nodemap->nodes[i].pnn));
5246 talloc_free(tmp_ctx);
5250 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5251 nodemap->nodes[i].pnn,
5252 "AllowClientDBAttach",
5255 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5256 "AllowClientDBAttach from node %u\n",
5257 nodemap->nodes[i].pnn));
5258 talloc_free(tmp_ctx);
5263 DEBUG(DEBUG_ERR, ("Database access is still active on "
5264 "node %u. Set AllowClientDBAttach=0 "
5266 nodemap->nodes[i].pnn));
5267 talloc_free(tmp_ctx);
5272 talloc_free(tmp_ctx);
5274 for (i=0; i<argc; i++) {
5275 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5279 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5280 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5281 "cannot be detached\n", argv[i]));
5286 ret = ctdb_detach(ctdb, db_id);
5288 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5300 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5302 struct ctdb_db_priority db_prio;
5309 db_prio.db_id = strtoul(argv[0], NULL, 0);
5310 db_prio.priority = strtoul(argv[1], NULL, 0);
5312 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5314 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5324 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5326 uint32_t db_id, priority;
5333 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5337 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5339 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5343 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5349 set the sticky records capability for a database
5351 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5353 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5361 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5365 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5367 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5368 talloc_free(tmp_ctx);
5372 talloc_free(tmp_ctx);
5377 set the readonly capability for a database
5379 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5381 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5389 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5393 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5395 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5396 talloc_free(tmp_ctx);
5400 talloc_free(tmp_ctx);
5407 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5417 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5421 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5423 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5427 printf("Sequence number:%lld\n", (long long)seqnum);
5433 run an eventscript on a node
5435 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5441 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5444 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5448 data.dptr = (unsigned char *)discard_const(argv[0]);
5449 data.dsize = strlen((char *)data.dptr) + 1;
5451 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5453 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5454 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5455 if (ret != 0 || res != 0) {
5456 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5457 talloc_free(tmp_ctx);
5460 talloc_free(tmp_ctx);
5464 #define DB_VERSION 1
5465 #define MAX_DB_NAME 64
5466 struct db_file_header {
5467 unsigned long version;
5469 unsigned long persistent;
5471 const char name[MAX_DB_NAME];
5474 struct backup_data {
5475 struct ctdb_marshall_buffer *records;
5478 bool traverse_error;
5481 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5483 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5484 struct ctdb_rec_data *rec;
5486 /* add the record */
5487 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5489 bd->traverse_error = true;
5490 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5493 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5494 if (bd->records == NULL) {
5495 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5496 bd->traverse_error = true;
5499 bd->records->count++;
5500 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5501 bd->len += rec->length;
5509 * backup a database to a file
5511 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5513 const char *db_name;
5515 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5516 struct db_file_header dbhdr;
5517 struct ctdb_db_context *ctdb_db;
5518 struct backup_data *bd;
5521 const char *reason = NULL;
5525 assert_single_node_only();
5528 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5532 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5536 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5537 db_id, tmp_ctx, &reason);
5539 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5541 talloc_free(tmp_ctx);
5545 uint32_t allow_unhealthy = 0;
5547 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5548 "AllowUnhealthyDBRead",
5551 if (allow_unhealthy != 1) {
5552 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5555 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5557 talloc_free(tmp_ctx);
5561 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5563 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5564 "tunnable AllowUnhealthyDBRead = %u\n",
5568 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5569 if (ctdb_db == NULL) {
5570 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5571 talloc_free(tmp_ctx);
5576 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5578 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5579 talloc_free(tmp_ctx);
5584 bd = talloc_zero(tmp_ctx, struct backup_data);
5586 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5587 talloc_free(tmp_ctx);
5591 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5592 if (bd->records == NULL) {
5593 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5594 talloc_free(tmp_ctx);
5598 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5599 bd->records->db_id = ctdb_db->db_id;
5600 /* traverse the database collecting all records */
5601 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5602 bd->traverse_error) {
5603 DEBUG(DEBUG_ERR,("Traverse error\n"));
5604 talloc_free(tmp_ctx);
5608 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5611 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5613 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5614 talloc_free(tmp_ctx);
5619 dbhdr.version = DB_VERSION;
5620 dbhdr.timestamp = time(NULL);
5621 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5622 dbhdr.size = bd->len;
5623 if (strlen(argv[0]) >= MAX_DB_NAME) {
5624 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5627 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5628 ret = write(fh, &dbhdr, sizeof(dbhdr));
5630 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5633 ret = write(fh, bd->records, bd->len);
5635 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5644 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5648 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5650 talloc_free(tmp_ctx);
5655 * restore a database from a file
5657 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5660 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5663 struct db_file_header dbhdr;
5664 struct ctdb_db_context *ctdb_db;
5665 struct ctdb_node_map *nodemap=NULL;
5666 struct ctdb_vnn_map *vnnmap=NULL;
5668 struct ctdb_control_wipe_database w;
5670 uint32_t generation;
5675 assert_single_node_only();
5677 if (argc < 1 || argc > 2) {
5678 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5682 fh = open(argv[0], O_RDONLY);
5684 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5685 talloc_free(tmp_ctx);
5689 read(fh, &dbhdr, sizeof(dbhdr));
5690 if (dbhdr.version != DB_VERSION) {
5691 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5693 talloc_free(tmp_ctx);
5697 dbname = discard_const(dbhdr.name);
5699 dbname = discard_const(argv[1]);
5702 outdata.dsize = dbhdr.size;
5703 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5704 if (outdata.dptr == NULL) {
5705 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5707 talloc_free(tmp_ctx);
5710 read(fh, outdata.dptr, outdata.dsize);
5713 tm = localtime(&dbhdr.timestamp);
5714 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5715 printf("Restoring database '%s' from backup @ %s\n",
5719 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5720 if (ctdb_db == NULL) {
5721 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5722 talloc_free(tmp_ctx);
5726 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5728 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5729 talloc_free(tmp_ctx);
5734 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5736 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5737 talloc_free(tmp_ctx);
5741 /* freeze all nodes */
5742 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5743 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5744 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5750 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5751 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5752 talloc_free(tmp_ctx);
5757 generation = vnnmap->generation;
5758 data.dptr = (void *)&generation;
5759 data.dsize = sizeof(generation);
5761 /* start a cluster wide transaction */
5762 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5763 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5765 TIMELIMIT(), false, data,
5768 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5773 w.db_id = ctdb_db->db_id;
5774 w.transaction_id = generation;
5776 data.dptr = (void *)&w;
5777 data.dsize = sizeof(w);
5779 /* wipe all the remote databases. */
5780 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5781 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5783 TIMELIMIT(), false, data,
5786 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5787 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5788 talloc_free(tmp_ctx);
5792 /* push the database */
5793 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5794 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5796 TIMELIMIT(), false, outdata,
5799 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5800 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5801 talloc_free(tmp_ctx);
5805 data.dptr = (void *)&ctdb_db->db_id;
5806 data.dsize = sizeof(ctdb_db->db_id);
5808 /* mark the database as healthy */
5809 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5810 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5812 TIMELIMIT(), false, data,
5815 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5816 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5817 talloc_free(tmp_ctx);
5821 data.dptr = (void *)&generation;
5822 data.dsize = sizeof(generation);
5824 /* commit all the changes */
5825 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5827 TIMELIMIT(), false, data,
5830 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5831 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5832 talloc_free(tmp_ctx);
5837 /* thaw all nodes */
5838 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5839 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5845 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5846 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5847 talloc_free(tmp_ctx);
5852 talloc_free(tmp_ctx);
5857 * dump a database backup from a file
5859 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5861 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5863 struct db_file_header dbhdr;
5867 struct ctdb_rec_data *rec = NULL;
5868 struct ctdb_marshall_buffer *m;
5869 struct ctdb_dump_db_context c;
5871 assert_single_node_only();
5874 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5878 fh = open(argv[0], O_RDONLY);
5880 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5881 talloc_free(tmp_ctx);
5885 read(fh, &dbhdr, sizeof(dbhdr));
5886 if (dbhdr.version != DB_VERSION) {
5887 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5889 talloc_free(tmp_ctx);
5893 outdata.dsize = dbhdr.size;
5894 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5895 if (outdata.dptr == NULL) {
5896 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5898 talloc_free(tmp_ctx);
5901 read(fh, outdata.dptr, outdata.dsize);
5903 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5905 tm = localtime(&dbhdr.timestamp);
5906 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5907 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5908 dbhdr.name, m->db_id, tbuf);
5912 c.printemptyrecords = (bool)options.printemptyrecords;
5913 c.printdatasize = (bool)options.printdatasize;
5914 c.printlmaster = false;
5915 c.printhash = (bool)options.printhash;
5916 c.printrecordflags = (bool)options.printrecordflags;
5918 for (i=0; i < m->count; i++) {
5922 /* we do not want the header splitted, so we pass NULL*/
5923 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5926 ctdb_dumpdb_record(ctdb, key, data, &c);
5929 printf("Dumped %d records\n", i);
5930 talloc_free(tmp_ctx);
5935 * wipe a database from a file
5937 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5940 const char *db_name;
5942 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5944 struct ctdb_db_context *ctdb_db;
5945 struct ctdb_node_map *nodemap = NULL;
5946 struct ctdb_vnn_map *vnnmap = NULL;
5948 struct ctdb_control_wipe_database w;
5950 uint32_t generation;
5953 assert_single_node_only();
5956 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5960 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5964 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5965 if (ctdb_db == NULL) {
5966 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5968 talloc_free(tmp_ctx);
5972 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5975 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5977 talloc_free(tmp_ctx);
5981 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5984 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5986 talloc_free(tmp_ctx);
5990 /* freeze all nodes */
5991 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5992 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5993 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
6000 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
6001 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
6002 CTDB_RECOVERY_ACTIVE);
6003 talloc_free(tmp_ctx);
6008 generation = vnnmap->generation;
6009 data.dptr = (void *)&generation;
6010 data.dsize = sizeof(generation);
6012 /* start a cluster wide transaction */
6013 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6014 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
6016 TIMELIMIT(), false, data,
6020 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
6021 "transactions.\n"));
6025 w.db_id = ctdb_db->db_id;
6026 w.transaction_id = generation;
6028 data.dptr = (void *)&w;
6029 data.dsize = sizeof(w);
6031 /* wipe all the remote databases. */
6032 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6033 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
6035 TIMELIMIT(), false, data,
6038 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
6039 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6040 talloc_free(tmp_ctx);
6044 data.dptr = (void *)&ctdb_db->db_id;
6045 data.dsize = sizeof(ctdb_db->db_id);
6047 /* mark the database as healthy */
6048 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6049 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
6051 TIMELIMIT(), false, data,
6054 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
6055 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6056 talloc_free(tmp_ctx);
6060 data.dptr = (void *)&generation;
6061 data.dsize = sizeof(generation);
6063 /* commit all the changes */
6064 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
6066 TIMELIMIT(), false, data,
6069 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
6070 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6071 talloc_free(tmp_ctx);
6075 /* thaw all nodes */
6076 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6077 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
6083 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
6084 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6085 talloc_free(tmp_ctx);
6089 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
6091 talloc_free(tmp_ctx);
6098 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6104 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6105 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6106 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6107 if (ret != 0 || res != 0) {
6108 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6109 talloc_free(tmp_ctx);
6112 write(1, data.dptr, data.dsize);
6113 talloc_free(tmp_ctx);
6118 handler for memory dumps
6120 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6121 TDB_DATA data, void *private_data)
6123 write(1, data.dptr, data.dsize);
6128 dump memory usage on the recovery daemon
6130 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6134 struct srvid_request rd;
6136 rd.pnn = ctdb_get_pnn(ctdb);
6137 rd.srvid = getpid();
6139 /* register a message port for receiveing the reply so that we
6140 can receive the reply
6142 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6145 data.dptr = (uint8_t *)&rd;
6146 data.dsize = sizeof(rd);
6148 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6150 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6154 /* this loop will terminate when we have received the reply */
6156 event_loop_once(ctdb->ev);
6163 send a message to a srvid
6165 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6167 unsigned long srvid;
6175 srvid = strtoul(argv[0], NULL, 0);
6177 data.dptr = (uint8_t *)discard_const(argv[1]);
6178 data.dsize= strlen(argv[1]);
6180 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6182 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6190 handler for msglisten
6192 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6193 TDB_DATA data, void *private_data)
6197 printf("Message received: ");
6198 for (i=0;i<data.dsize;i++) {
6199 printf("%c", data.dptr[i]);
6205 listen for messages on a messageport
6207 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6213 /* register a message port and listen for messages
6215 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6216 printf("Listening for messages on srvid:%d\n", (int)srvid);
6219 event_loop_once(ctdb->ev);
6226 list all nodes in the cluster
6227 we parse the nodes file directly
6229 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6231 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6232 struct pnn_node *pnn_nodes;
6233 struct pnn_node *pnn_node;
6235 assert_single_node_only();
6237 pnn_nodes = read_nodes_file(mem_ctx);
6238 if (pnn_nodes == NULL) {
6239 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6240 talloc_free(mem_ctx);
6244 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6245 const char *addr = ctdb_addr_to_str(&pnn_node->addr);
6246 if (options.machinereadable){
6247 printf(":%d:%s:\n", pnn_node->pnn, addr);
6249 printf("%s\n", addr);
6252 talloc_free(mem_ctx);
6258 reload the nodes file on the local node
6260 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6264 struct ctdb_node_map *nodemap=NULL;
6266 assert_single_node_only();
6268 mypnn = ctdb_get_pnn(ctdb);
6270 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6272 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6276 /* reload the nodes file on all remote nodes */
6277 for (i=0;i<nodemap->num;i++) {
6278 if (nodemap->nodes[i].pnn == mypnn) {
6281 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6282 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6283 nodemap->nodes[i].pnn);
6285 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6289 /* reload the nodes file on the local node */
6290 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6291 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6293 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6296 /* initiate a recovery */
6297 control_recover(ctdb, argc, argv);
6303 static const struct {
6305 int (*fn)(struct ctdb_context *, int, const char **);
6307 bool without_daemon; /* can be run without daemon running ? */
6310 } ctdb_commands[] = {
6311 { "version", control_version, true, true, "show version of ctdb" },
6312 { "status", control_status, true, false, "show node status" },
6313 { "uptime", control_uptime, true, false, "show node uptime" },
6314 { "ping", control_ping, true, false, "ping all nodes" },
6315 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6316 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6317 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6318 { "listvars", control_listvars, true, false, "list tunable variables"},
6319 { "statistics", control_statistics, false, false, "show statistics" },
6320 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6321 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6322 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6323 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6324 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6325 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6326 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6327 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6328 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6329 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6330 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6331 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6332 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6333 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6334 { "lvs", control_lvs, true, false, "show lvs configuration" },
6335 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6336 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6337 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6338 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6339 { "getdebug", control_getdebug, true, false, "get debug level" },
6340 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6341 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6342 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6343 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6344 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6345 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6346 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6347 { "disable", control_disable, true, false, "disable a nodes public IP" },
6348 { "enable", control_enable, true, false, "enable a nodes public IP" },
6349 { "stop", control_stop, true, false, "stop a node" },
6350 { "continue", control_continue, true, false, "re-start a stopped node" },
6351 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6352 { "unban", control_unban, true, false, "unban a node" },
6353 { "showban", control_showban, true, false, "show ban information"},
6354 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6355 { "recover", control_recover, true, false, "force recovery" },
6356 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6357 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6358 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6359 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6360 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6361 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6362 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6363 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6364 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6366 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6368 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6369 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6370 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6371 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6372 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6373 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6374 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6375 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6376 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6377 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6378 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6379 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6380 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6381 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6382 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6383 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6384 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6385 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6386 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6387 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6388 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6389 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6390 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6391 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6392 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6393 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6394 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6395 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6396 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6397 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6398 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6399 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6400 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6401 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6402 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6403 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6404 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6405 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6406 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6407 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6408 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6409 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6410 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6411 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6412 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6413 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6414 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6415 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6416 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6422 static void usage(void)
6426 "Usage: ctdb [options] <control>\n" \
6428 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6429 " -Y generate machinereadable output\n"
6430 " -v generate verbose output\n"
6431 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6432 printf("Controls:\n");
6433 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6434 printf(" %-15s %-27s %s\n",
6435 ctdb_commands[i].name,
6436 ctdb_commands[i].args?ctdb_commands[i].args:"",
6437 ctdb_commands[i].msg);
6443 static void ctdb_alarm(int sig)
6445 printf("Maximum runtime exceeded - exiting\n");
6452 int main(int argc, const char *argv[])
6454 struct ctdb_context *ctdb;
6455 char *nodestring = NULL;
6456 struct poptOption popt_options[] = {
6459 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6460 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6461 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6462 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6463 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6464 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6465 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6466 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6467 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6468 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6472 const char **extra_argv;
6476 struct event_context *ev;
6477 const char *control;
6481 /* set some defaults */
6482 options.maxruntime = 0;
6483 options.timelimit = 10;
6484 options.pnn = CTDB_CURRENT_NODE;
6486 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6488 while ((opt = poptGetNextOpt(pc)) != -1) {
6491 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6492 poptBadOption(pc, 0), poptStrerror(opt)));
6497 /* setup the remaining options for the main program to use */
6498 extra_argv = poptGetArgs(pc);
6501 while (extra_argv[extra_argc]) extra_argc++;
6504 if (extra_argc < 1) {
6508 if (options.maxruntime == 0) {
6509 const char *ctdb_timeout;
6510 ctdb_timeout = getenv("CTDB_TIMEOUT");
6511 if (ctdb_timeout != NULL) {
6512 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6514 /* default timeout is 120 seconds */
6515 options.maxruntime = 120;
6519 signal(SIGALRM, ctdb_alarm);
6520 alarm(options.maxruntime);
6522 control = extra_argv[0];
6524 /* Default value for CTDB_BASE - don't override */
6525 setenv("CTDB_BASE", ETCDIR "/ctdb", 0);
6527 ev = event_context_init(NULL);
6529 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6533 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6534 if (strcmp(control, ctdb_commands[i].name) == 0) {
6539 if (i == ARRAY_SIZE(ctdb_commands)) {
6540 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6544 if (ctdb_commands[i].without_daemon == true) {
6545 if (nodestring != NULL) {
6546 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6549 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6552 /* initialise ctdb */
6553 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6556 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6560 /* setup the node number(s) to contact */
6561 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6562 &options.nodes, &options.pnn)) {
6566 if (options.pnn == CTDB_CURRENT_NODE) {
6567 options.pnn = options.nodes[0];
6570 if (ctdb_commands[i].auto_all &&
6571 ((options.pnn == CTDB_BROADCAST_ALL) ||
6572 (options.pnn == CTDB_MULTICAST))) {
6576 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6577 options.pnn = options.nodes[j];
6578 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6581 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6586 (void)poptFreeContext(pc);