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"
34 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
35 #define ERR_NONODE 21 /* node does not exist */
36 #define ERR_DISNODE 22 /* node is disconnected */
38 static void usage(void);
47 int printemptyrecords;
54 #define LONGTIMEOUT options.timelimit*10
56 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
57 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
59 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
61 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
65 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
66 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
67 "Out of memory in " __location__ )); \
71 static uint32_t getpnn(struct ctdb_context *ctdb)
73 if ((options.pnn == CTDB_BROADCAST_ALL) ||
74 (options.pnn == CTDB_MULTICAST)) {
76 ("Cannot get PNN for node %u\n", options.pnn));
80 if (options.pnn == CTDB_CURRENT_NODE) {
81 return ctdb_get_pnn(ctdb);
87 static void assert_single_node_only(void)
89 if ((options.pnn == CTDB_BROADCAST_ALL) ||
90 (options.pnn == CTDB_MULTICAST)) {
92 ("This control can not be applied to multiple PNNs\n"));
97 /* Pretty print the flags to a static buffer in human-readable format.
98 * This never returns NULL!
100 static const char *pretty_print_flags(uint32_t flags)
103 static const struct {
107 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
108 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
109 { NODE_FLAGS_BANNED, "BANNED" },
110 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
111 { NODE_FLAGS_DELETED, "DELETED" },
112 { NODE_FLAGS_STOPPED, "STOPPED" },
113 { NODE_FLAGS_INACTIVE, "INACTIVE" },
115 static char flags_str[512]; /* Big enough to contain all flag names */
118 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
119 if (flags & flag_names[j].flag) {
120 if (flags_str[0] == '\0') {
121 (void) strcpy(flags_str, flag_names[j].name);
123 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
124 (void) strncat(flags_str, flag_names[j].name,
125 sizeof(flags_str)-1);
129 if (flags_str[0] == '\0') {
130 (void) strcpy(flags_str, "OK");
136 static int h2i(char h)
138 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
139 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
143 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
146 TDB_DATA key = {NULL, 0};
150 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
155 key.dptr = talloc_size(mem_ctx, key.dsize);
157 for (i=0; i < len/2; i++) {
158 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
163 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
164 * that are disconnected or deleted. If dd_ok is true those nodes are
165 * included in the output list of nodes. If dd_ok is false, those
166 * nodes are filtered from the "all" case and cause an error if
167 * explicitly specified.
169 static bool parse_nodestring(struct ctdb_context *ctdb,
171 const char * nodestring,
172 uint32_t current_pnn,
177 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
180 struct ctdb_node_map *nodemap;
185 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
187 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
188 talloc_free(tmp_ctx);
192 if (nodestring != NULL) {
193 *nodes = talloc_array(mem_ctx, uint32_t, 0);
194 if (*nodes == NULL) {
200 if (strcmp(nodestring, "all") == 0) {
201 *pnn_mode = CTDB_BROADCAST_ALL;
204 for (i = 0; i < nodemap->num; i++) {
205 if ((nodemap->nodes[i].flags &
206 (NODE_FLAGS_DISCONNECTED |
207 NODE_FLAGS_DELETED)) && !dd_ok) {
210 *nodes = talloc_realloc(mem_ctx, *nodes,
212 if (*nodes == NULL) {
222 ns = talloc_strdup(tmp_ctx, nodestring);
223 tok = strtok(ns, ",");
224 while (tok != NULL) {
227 i = (uint32_t)strtoul(tok, &endptr, 0);
228 if (i == 0 && tok == endptr) {
230 ("Invalid node %s\n", tok));
231 talloc_free(tmp_ctx);
234 if (i >= nodemap->num) {
235 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
236 talloc_free(tmp_ctx);
239 if ((nodemap->nodes[i].flags &
240 (NODE_FLAGS_DISCONNECTED |
241 NODE_FLAGS_DELETED)) && !dd_ok) {
242 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
243 talloc_free(tmp_ctx);
246 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
247 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
248 talloc_free(tmp_ctx);
252 *nodes = talloc_realloc(mem_ctx, *nodes,
254 if (*nodes == NULL) {
261 tok = strtok(NULL, ",");
266 *pnn_mode = (*nodes)[0];
268 *pnn_mode = CTDB_MULTICAST;
272 /* default - no nodes specified */
273 *nodes = talloc_array(mem_ctx, uint32_t, 1);
274 if (*nodes == NULL) {
277 *pnn_mode = CTDB_CURRENT_NODE;
279 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
284 talloc_free(tmp_ctx);
288 talloc_free(tmp_ctx);
293 check if a database exists
295 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
296 uint32_t *dbid, const char **dbname, uint8_t *flags)
299 struct ctdb_dbid_map *dbmap=NULL;
300 bool dbid_given = false, found = false;
302 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
305 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
307 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
311 if (strncmp(dbarg, "0x", 2) == 0) {
312 id = strtoul(dbarg, NULL, 0);
316 for(i=0; i<dbmap->num; i++) {
318 if (id == dbmap->dbs[i].dbid) {
323 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
325 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
329 if (strcmp(name, dbarg) == 0) {
330 id = dbmap->dbs[i].dbid;
337 if (found && dbid_given && dbname != NULL) {
338 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
340 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
347 if (dbid) *dbid = id;
348 if (dbname) *dbname = talloc_strdup(ctdb, name);
349 if (flags) *flags = dbmap->dbs[i].flags;
351 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
355 talloc_free(tmp_ctx);
360 see if a process exists
362 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
370 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
371 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
375 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
377 printf("%u:%u exists\n", pnn, pid);
379 printf("%u:%u does not exist\n", pnn, pid);
385 display statistics structure
387 static void show_statistics(struct ctdb_statistics *s, int show_header)
389 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
391 const char *prefix=NULL;
393 int tmp, days, hours, minutes, seconds;
398 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
399 STATISTICS_FIELD(num_clients),
400 STATISTICS_FIELD(frozen),
401 STATISTICS_FIELD(recovering),
402 STATISTICS_FIELD(num_recoveries),
403 STATISTICS_FIELD(client_packets_sent),
404 STATISTICS_FIELD(client_packets_recv),
405 STATISTICS_FIELD(node_packets_sent),
406 STATISTICS_FIELD(node_packets_recv),
407 STATISTICS_FIELD(keepalive_packets_sent),
408 STATISTICS_FIELD(keepalive_packets_recv),
409 STATISTICS_FIELD(node.req_call),
410 STATISTICS_FIELD(node.reply_call),
411 STATISTICS_FIELD(node.req_dmaster),
412 STATISTICS_FIELD(node.reply_dmaster),
413 STATISTICS_FIELD(node.reply_error),
414 STATISTICS_FIELD(node.req_message),
415 STATISTICS_FIELD(node.req_control),
416 STATISTICS_FIELD(node.reply_control),
417 STATISTICS_FIELD(client.req_call),
418 STATISTICS_FIELD(client.req_message),
419 STATISTICS_FIELD(client.req_control),
420 STATISTICS_FIELD(timeouts.call),
421 STATISTICS_FIELD(timeouts.control),
422 STATISTICS_FIELD(timeouts.traverse),
423 STATISTICS_FIELD(locks.num_calls),
424 STATISTICS_FIELD(locks.num_current),
425 STATISTICS_FIELD(locks.num_pending),
426 STATISTICS_FIELD(locks.num_failed),
427 STATISTICS_FIELD(total_calls),
428 STATISTICS_FIELD(pending_calls),
429 STATISTICS_FIELD(childwrite_calls),
430 STATISTICS_FIELD(pending_childwrite_calls),
431 STATISTICS_FIELD(memory_used),
432 STATISTICS_FIELD(max_hop_count),
433 STATISTICS_FIELD(total_ro_delegations),
434 STATISTICS_FIELD(total_ro_revokes),
437 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
446 if (options.machinereadable){
448 printf("CTDB version:");
449 printf("Current time of statistics:");
450 printf("Statistics collected since:");
451 for (i=0;i<ARRAY_SIZE(fields);i++) {
452 printf("%s:", fields[i].name);
454 printf("num_reclock_ctdbd_latency:");
455 printf("min_reclock_ctdbd_latency:");
456 printf("avg_reclock_ctdbd_latency:");
457 printf("max_reclock_ctdbd_latency:");
459 printf("num_reclock_recd_latency:");
460 printf("min_reclock_recd_latency:");
461 printf("avg_reclock_recd_latency:");
462 printf("max_reclock_recd_latency:");
464 printf("num_call_latency:");
465 printf("min_call_latency:");
466 printf("avg_call_latency:");
467 printf("max_call_latency:");
469 printf("num_lockwait_latency:");
470 printf("min_lockwait_latency:");
471 printf("avg_lockwait_latency:");
472 printf("max_lockwait_latency:");
474 printf("num_childwrite_latency:");
475 printf("min_childwrite_latency:");
476 printf("avg_childwrite_latency:");
477 printf("max_childwrite_latency:");
480 printf("%d:", CTDB_VERSION);
481 printf("%d:", (int)s->statistics_current_time.tv_sec);
482 printf("%d:", (int)s->statistics_start_time.tv_sec);
483 for (i=0;i<ARRAY_SIZE(fields);i++) {
484 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
486 printf("%d:", s->reclock.ctdbd.num);
487 printf("%.6f:", s->reclock.ctdbd.min);
488 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
489 printf("%.6f:", s->reclock.ctdbd.max);
491 printf("%d:", s->reclock.recd.num);
492 printf("%.6f:", s->reclock.recd.min);
493 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
494 printf("%.6f:", s->reclock.recd.max);
496 printf("%d:", s->call_latency.num);
497 printf("%.6f:", s->call_latency.min);
498 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
499 printf("%.6f:", s->call_latency.max);
501 printf("%d:", s->childwrite_latency.num);
502 printf("%.6f:", s->childwrite_latency.min);
503 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
504 printf("%.6f:", s->childwrite_latency.max);
507 printf("CTDB version %u\n", CTDB_VERSION);
508 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
509 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
511 for (i=0;i<ARRAY_SIZE(fields);i++) {
512 if (strchr(fields[i].name, '.')) {
513 preflen = strcspn(fields[i].name, ".")+1;
514 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
515 prefix = fields[i].name;
516 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
521 printf(" %*s%-22s%*s%10u\n",
523 fields[i].name+preflen,
525 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
527 printf(" hop_count_buckets:");
528 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
529 printf(" %d", s->hop_count_bucket[i]);
532 printf(" lock_buckets:");
533 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
534 printf(" %d", s->locks.buckets[i]);
537 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);
539 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);
541 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);
543 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);
544 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);
547 talloc_free(tmp_ctx);
551 display remote ctdb statistics combined from all nodes
553 static int control_statistics_all(struct ctdb_context *ctdb)
556 struct ctdb_statistics statistics;
560 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
561 CTDB_NO_MEMORY(ctdb, nodes);
563 ZERO_STRUCT(statistics);
565 for (i=0;i<num_nodes;i++) {
566 struct ctdb_statistics s1;
568 uint32_t *v1 = (uint32_t *)&s1;
569 uint32_t *v2 = (uint32_t *)&statistics;
571 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
572 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
574 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
577 for (j=0;j<num_ints;j++) {
580 statistics.max_hop_count =
581 MAX(statistics.max_hop_count, s1.max_hop_count);
582 statistics.call_latency.max =
583 MAX(statistics.call_latency.max, s1.call_latency.max);
586 printf("Gathered statistics for %u nodes\n", num_nodes);
587 show_statistics(&statistics, 1);
592 display remote ctdb statistics
594 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
597 struct ctdb_statistics statistics;
599 if (options.pnn == CTDB_BROADCAST_ALL) {
600 return control_statistics_all(ctdb);
603 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
605 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
608 show_statistics(&statistics, 1);
614 reset remote ctdb statistics
616 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
620 ret = ctdb_statistics_reset(ctdb, options.pnn);
622 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
630 display remote ctdb rolling statistics
632 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
635 struct ctdb_statistics_wire *stats;
636 int i, num_records = -1;
638 assert_single_node_only();
641 num_records = atoi(argv[0]) - 1;
644 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
646 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
649 for (i=0;i<stats->num;i++) {
650 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
653 show_statistics(&stats->stats[i], i==0);
654 if (i == num_records) {
663 display remote ctdb db statistics
665 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
667 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
668 struct ctdb_db_statistics *dbstat;
678 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
682 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
684 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
685 talloc_free(tmp_ctx);
689 printf("DB Statistics: %s\n", argv[0]);
690 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
691 dbstat->db_ro_delegations);
692 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
693 dbstat->db_ro_delegations);
694 printf(" %s\n", "locks");
695 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
696 dbstat->locks.num_calls);
697 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
698 dbstat->locks.num_failed);
699 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
700 dbstat->locks.num_current);
701 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
702 dbstat->locks.num_pending);
703 printf(" %s", "hop_count_buckets:");
704 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
705 printf(" %d", dbstat->hop_count_bucket[i]);
708 printf(" %s", "lock_buckets:");
709 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
710 printf(" %d", dbstat->locks.buckets[i]);
713 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
714 "locks_latency MIN/AVG/MAX",
715 dbstat->locks.latency.min,
716 (dbstat->locks.latency.num ?
717 dbstat->locks.latency.total /dbstat->locks.latency.num :
719 dbstat->locks.latency.max,
720 dbstat->locks.latency.num);
722 for (i=0; i<dbstat->num_hot_keys; i++) {
723 if (dbstat->hot_keys[i].count > 0) {
727 dbstat->num_hot_keys = num_hot_keys;
729 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
730 for (i = 0; i < dbstat->num_hot_keys; i++) {
732 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
733 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
734 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
739 talloc_free(tmp_ctx);
744 display uptime of remote node
746 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
749 struct ctdb_uptime *uptime = NULL;
750 int tmp, days, hours, minutes, seconds;
752 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
754 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
758 if (options.machinereadable){
759 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
760 printf(":%u:%u:%u:%lf\n",
761 (unsigned int)uptime->current_time.tv_sec,
762 (unsigned int)uptime->ctdbd_start_time.tv_sec,
763 (unsigned int)uptime->last_recovery_finished.tv_sec,
764 timeval_delta(&uptime->last_recovery_finished,
765 &uptime->last_recovery_started)
770 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
772 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
780 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
782 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
790 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
792 printf("Duration of last recovery/failover: %lf seconds\n",
793 timeval_delta(&uptime->last_recovery_finished,
794 &uptime->last_recovery_started));
800 show the PNN of the current node
802 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
806 mypnn = getpnn(ctdb);
808 printf("PNN:%d\n", mypnn);
814 struct pnn_node *next;
819 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
821 const char *nodes_list;
825 struct pnn_node *pnn_nodes = NULL;
826 struct pnn_node *pnn_node;
827 struct pnn_node *tmp_node;
829 /* read the nodes file */
830 nodes_list = getenv("CTDB_NODES");
831 if (nodes_list == NULL) {
832 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
833 getenv("CTDB_BASE"));
834 if (nodes_list == NULL) {
835 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
839 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
843 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
846 for (i=0, pnn=0; i<nlines; i++) {
850 /* strip leading spaces */
851 while((*node == ' ') || (*node == '\t')) {
858 if (strcmp(node, "") == 0) {
861 pnn_node = talloc(mem_ctx, struct pnn_node);
862 pnn_node->pnn = pnn++;
863 pnn_node->addr = talloc_strdup(pnn_node, node);
864 pnn_node->next = pnn_nodes;
865 pnn_nodes = pnn_node;
868 /* swap them around so we return them in incrementing order */
869 pnn_node = pnn_nodes;
873 pnn_node = pnn_node->next;
875 tmp_node->next = pnn_nodes;
876 pnn_nodes = tmp_node;
883 show the PNN of the current node
884 discover the pnn by loading the nodes file and try to bind to all
885 addresses one at a time until the ip address is found.
887 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
889 TALLOC_CTX *mem_ctx = talloc_new(NULL);
890 struct pnn_node *pnn_nodes;
891 struct pnn_node *pnn_node;
893 assert_single_node_only();
895 pnn_nodes = read_nodes_file(mem_ctx);
896 if (pnn_nodes == NULL) {
897 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
898 talloc_free(mem_ctx);
902 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
905 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
906 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
907 talloc_free(mem_ctx);
911 if (ctdb_sys_have_ip(&addr)) {
912 printf("PNN:%d\n", pnn_node->pnn);
913 talloc_free(mem_ctx);
918 printf("Failed to detect which PNN this node is\n");
919 talloc_free(mem_ctx);
923 /* Helpers for ctdb status
925 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
927 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
931 if (node->flags == 0) {
932 struct ctdb_control_get_ifaces *ifaces;
934 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
935 tmp_ctx, &ifaces) == 0) {
936 for (j=0; j < ifaces->num; j++) {
937 if (ifaces->ifaces[j].link_state != 0) {
945 talloc_free(tmp_ctx);
950 static void control_status_header_machine(void)
952 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
953 ":Inactive:PartiallyOnline:ThisNode:\n");
956 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
957 struct ctdb_node_and_flags *node)
959 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
960 ctdb_addr_to_str(&node->addr),
961 !!(node->flags&NODE_FLAGS_DISCONNECTED),
962 !!(node->flags&NODE_FLAGS_BANNED),
963 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
964 !!(node->flags&NODE_FLAGS_UNHEALTHY),
965 !!(node->flags&NODE_FLAGS_STOPPED),
966 !!(node->flags&NODE_FLAGS_INACTIVE),
967 is_partially_online(ctdb, node) ? 1 : 0,
968 (node->pnn == mypnn)?'Y':'N');
973 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
974 struct ctdb_node_and_flags *node)
976 printf("pnn:%d %-16s %s%s\n", node->pnn,
977 ctdb_addr_to_str(&node->addr),
978 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
979 node->pnn == mypnn?" (THIS NODE)":"");
985 display remote ctdb status
987 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
989 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
991 struct ctdb_vnn_map *vnnmap=NULL;
992 struct ctdb_node_map *nodemap=NULL;
993 uint32_t recmode, recmaster, mypnn;
994 int num_deleted_nodes = 0;
997 mypnn = getpnn(ctdb);
999 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1001 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1002 talloc_free(tmp_ctx);
1006 if (options.machinereadable) {
1007 control_status_header_machine();
1008 for (i=0;i<nodemap->num;i++) {
1009 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1012 (void) control_status_1_machine(ctdb, mypnn,
1013 &nodemap->nodes[i]);
1015 talloc_free(tmp_ctx);
1019 for (i=0; i<nodemap->num; i++) {
1020 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1021 num_deleted_nodes++;
1024 if (num_deleted_nodes == 0) {
1025 printf("Number of nodes:%d\n", nodemap->num);
1027 printf("Number of nodes:%d (including %d deleted nodes)\n",
1028 nodemap->num, num_deleted_nodes);
1030 for(i=0;i<nodemap->num;i++){
1031 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1034 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1037 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1039 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1040 talloc_free(tmp_ctx);
1043 if (vnnmap->generation == INVALID_GENERATION) {
1044 printf("Generation:INVALID\n");
1046 printf("Generation:%d\n",vnnmap->generation);
1048 printf("Size:%d\n",vnnmap->size);
1049 for(i=0;i<vnnmap->size;i++){
1050 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1053 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1055 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1056 talloc_free(tmp_ctx);
1059 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1061 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1063 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1064 talloc_free(tmp_ctx);
1067 printf("Recovery master:%d\n",recmaster);
1069 talloc_free(tmp_ctx);
1073 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1075 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1077 struct ctdb_node_map *nodemap=NULL;
1079 uint32_t pnn_mode, mypnn;
1085 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1086 options.pnn, true, &nodes, &pnn_mode)) {
1090 if (options.machinereadable) {
1091 control_status_header_machine();
1092 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1093 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1096 mypnn = getpnn(ctdb);
1098 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1100 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1101 talloc_free(tmp_ctx);
1107 for (i = 0; i < talloc_array_length(nodes); i++) {
1108 if (options.machinereadable) {
1109 ret |= control_status_1_machine(ctdb, mypnn,
1110 &nodemap->nodes[nodes[i]]);
1112 ret |= control_status_1_human(ctdb, mypnn,
1113 &nodemap->nodes[nodes[i]]);
1117 talloc_free(tmp_ctx);
1122 struct natgw_node *next;
1126 /* talloc off the existing nodemap... */
1127 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1129 return talloc_zero_size(nodemap,
1130 offsetof(struct ctdb_node_map, nodes) +
1131 nodemap->num * sizeof(struct ctdb_node_and_flags));
1134 static struct ctdb_node_map *
1135 filter_nodemap_by_natgw_nodes(struct ctdb_context *ctdb,
1136 struct ctdb_node_map *nodemap,
1137 struct natgw_node *natgw_nodes)
1140 struct natgw_node *n;
1141 struct ctdb_node_map *ret;
1143 ret = talloc_nodemap(nodemap);
1144 CTDB_NO_MEMORY_NULL(ctdb, ret);
1148 for (i = 0; i < nodemap->num; i++) {
1149 for(n = natgw_nodes; n != NULL ; n = n->next) {
1150 if (!strcmp(n->addr,
1151 ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1159 ret->nodes[ret->num] = nodemap->nodes[i];
1166 static struct ctdb_node_map *
1167 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1168 struct ctdb_node_map *nodemap,
1169 uint32_t required_capabilities)
1172 uint32_t capabilities;
1173 struct ctdb_node_map *ret;
1175 ret = talloc_nodemap(nodemap);
1176 CTDB_NO_MEMORY_NULL(ctdb, ret);
1180 for (i = 0; i < nodemap->num; i++) {
1181 int res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1182 nodemap->nodes[i].pnn,
1185 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1186 nodemap->nodes[i].pnn));
1190 if (!(capabilities & required_capabilities)) {
1194 ret->nodes[ret->num] = nodemap->nodes[i];
1201 static struct ctdb_node_map *
1202 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1203 struct ctdb_node_map *nodemap,
1204 uint32_t flags_mask)
1207 struct ctdb_node_map *ret;
1209 ret = talloc_nodemap(nodemap);
1210 CTDB_NO_MEMORY_NULL(ctdb, ret);
1214 for (i = 0; i < nodemap->num; i++) {
1215 if (nodemap->nodes[i].flags & flags_mask) {
1219 ret->nodes[ret->num] = nodemap->nodes[i];
1227 display the list of nodes belonging to this natgw configuration
1229 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1231 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1233 const char *natgw_list;
1236 struct natgw_node *natgw_nodes = NULL;
1237 struct natgw_node *natgw_node;
1238 struct ctdb_node_map *orig_nodemap=NULL;
1239 struct ctdb_node_map *cnodemap, *nodemap;
1240 uint32_t mypnn, pnn;
1243 /* When we have some nodes that could be the NATGW, make a
1244 * series of attempts to find the first node that doesn't have
1245 * certain status flags set.
1247 uint32_t exclude_flags[] = {
1248 /* Look for a nice healthy node */
1249 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1250 /* If not found, an UNHEALTHY/BANNED node will do */
1251 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1252 /* If not found, a STOPPED node will do */
1253 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1257 /* read the natgw nodes file into a linked list */
1258 natgw_list = getenv("CTDB_NATGW_NODES");
1259 if (natgw_list == NULL) {
1260 natgw_list = talloc_asprintf(tmp_ctx, "%s/natgw_nodes",
1261 getenv("CTDB_BASE"));
1262 if (natgw_list == NULL) {
1263 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1267 lines = file_lines_load(natgw_list, &nlines, ctdb);
1268 if (lines == NULL) {
1269 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
1270 talloc_free(tmp_ctx);
1273 for (i=0;i<nlines;i++) {
1277 /* strip leading spaces */
1278 while((*node == ' ') || (*node == '\t')) {
1284 if (strcmp(node, "") == 0) {
1287 natgw_node = talloc(ctdb, struct natgw_node);
1288 natgw_node->addr = talloc_strdup(natgw_node, node);
1289 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1290 natgw_node->next = natgw_nodes;
1291 natgw_nodes = natgw_node;
1294 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1295 tmp_ctx, &orig_nodemap);
1297 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1298 talloc_free(tmp_ctx);
1302 /* Get a nodemap that includes only the nodes in the NATGW
1304 nodemap = filter_nodemap_by_natgw_nodes(ctdb, orig_nodemap,
1306 if (nodemap == NULL) {
1311 /* Get a nodemap that includes only the nodes with the NATGW
1313 cnodemap = filter_nodemap_by_capabilities(ctdb, nodemap,
1315 if (cnodemap == NULL) {
1320 ret = 2; /* matches ENOENT */
1323 for (i = 0; exclude_flags[i] != 0; i++) {
1324 struct ctdb_node_map *t =
1325 filter_nodemap_by_flags(ctdb, cnodemap,
1334 pnn = t->nodes[0].pnn;
1335 ip = ctdb_addr_to_str(&t->nodes[0].addr);
1341 if (options.machinereadable) {
1342 printf(":Node:IP:\n");
1343 printf(":%d:%s:\n", pnn, ip);
1345 printf("%d %s\n", pnn, ip);
1348 /* print the pruned list of nodes belonging to this natgw list */
1349 mypnn = getpnn(ctdb);
1350 if (options.machinereadable) {
1351 control_status_header_machine();
1353 printf("Number of nodes:%d\n", nodemap->num);
1355 for(i=0;i<nodemap->num;i++){
1356 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1359 if (options.machinereadable) {
1360 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1362 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1367 talloc_free(tmp_ctx);
1372 display the status of the scripts for monitoring (or other events)
1374 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1375 enum ctdb_eventscript_call type)
1377 struct ctdb_scripts_wire *script_status;
1380 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1382 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1386 if (script_status == NULL) {
1387 if (!options.machinereadable) {
1388 printf("%s cycle never run\n",
1389 ctdb_eventscript_call_names[type]);
1394 if (!options.machinereadable) {
1395 printf("%d scripts were executed last %s cycle\n",
1396 script_status->num_scripts,
1397 ctdb_eventscript_call_names[type]);
1399 for (i=0; i<script_status->num_scripts; i++) {
1400 const char *status = NULL;
1402 switch (script_status->scripts[i].status) {
1404 status = "TIMEDOUT";
1407 status = "DISABLED";
1413 if (script_status->scripts[i].status > 0)
1417 if (options.machinereadable) {
1418 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1419 ctdb_eventscript_call_names[type],
1420 script_status->scripts[i].name,
1421 script_status->scripts[i].status,
1423 (long)script_status->scripts[i].start.tv_sec,
1424 (long)script_status->scripts[i].start.tv_usec,
1425 (long)script_status->scripts[i].finished.tv_sec,
1426 (long)script_status->scripts[i].finished.tv_usec,
1427 script_status->scripts[i].output);
1431 printf("%-20s Status:%s ",
1432 script_status->scripts[i].name, status);
1434 /* Some other error, eg from stat. */
1435 printf("%-20s Status:CANNOT RUN (%s)",
1436 script_status->scripts[i].name,
1437 strerror(-script_status->scripts[i].status));
1439 if (script_status->scripts[i].status >= 0) {
1440 printf("Duration:%.3lf ",
1441 timeval_delta(&script_status->scripts[i].finished,
1442 &script_status->scripts[i].start));
1444 if (script_status->scripts[i].status != -ENOEXEC) {
1446 ctime(&script_status->scripts[i].start.tv_sec));
1447 if (script_status->scripts[i].status != 0) {
1448 printf(" OUTPUT:%s\n",
1449 script_status->scripts[i].output);
1459 static int control_scriptstatus(struct ctdb_context *ctdb,
1460 int argc, const char **argv)
1463 enum ctdb_eventscript_call type, min, max;
1467 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1472 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1476 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1477 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1483 if (type == CTDB_EVENT_MAX) {
1484 if (strcmp(arg, "all") == 0) {
1486 max = CTDB_EVENT_MAX;
1488 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1493 if (options.machinereadable) {
1494 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1497 for (type = min; type < max; type++) {
1498 ret = control_one_scriptstatus(ctdb, type);
1508 enable an eventscript
1510 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1518 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1520 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1528 disable an eventscript
1530 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1538 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1540 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1548 display the pnn of the recovery master
1550 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1555 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1557 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1560 printf("%d\n",recmaster);
1566 add a tickle to a public address
1568 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1570 struct ctdb_tcp_connection t;
1574 assert_single_node_only();
1580 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1581 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1584 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1585 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1589 data.dptr = (uint8_t *)&t;
1590 data.dsize = sizeof(t);
1592 /* tell all nodes about this tcp connection */
1593 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1594 0, data, ctdb, NULL, NULL, NULL, NULL);
1596 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1605 delete a tickle from a node
1607 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1609 struct ctdb_tcp_connection t;
1613 assert_single_node_only();
1619 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1620 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1623 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1624 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1628 data.dptr = (uint8_t *)&t;
1629 data.dsize = sizeof(t);
1631 /* tell all nodes about this tcp connection */
1632 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1633 0, data, ctdb, NULL, NULL, NULL, NULL);
1635 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1644 get a list of all tickles for this pnn
1646 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1648 struct ctdb_control_tcp_tickle_list *list;
1649 ctdb_sock_addr addr;
1653 assert_single_node_only();
1660 port = atoi(argv[1]);
1663 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1664 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1668 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1670 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1674 if (options.machinereadable){
1675 printf(":source ip:port:destination ip:port:\n");
1676 for (i=0;i<list->tickles.num;i++) {
1677 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1680 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1681 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1684 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1685 printf("Num tickles:%u\n", list->tickles.num);
1686 for (i=0;i<list->tickles.num;i++) {
1687 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1690 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1691 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));
1701 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1703 struct ctdb_all_public_ips *ips;
1704 struct ctdb_public_ip ip;
1707 uint32_t disable_time;
1709 struct ctdb_node_map *nodemap=NULL;
1710 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1713 data.dptr = (uint8_t*)&disable_time;
1714 data.dsize = sizeof(disable_time);
1715 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1717 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1723 /* read the public ip list from the node */
1724 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1726 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1727 talloc_free(tmp_ctx);
1731 for (i=0;i<ips->num;i++) {
1732 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1737 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1738 pnn, ctdb_addr_to_str(addr)));
1739 talloc_free(tmp_ctx);
1746 data.dptr = (uint8_t *)&ip;
1747 data.dsize = sizeof(ip);
1749 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1751 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1752 talloc_free(tmp_ctx);
1756 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1757 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1764 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1765 talloc_free(tmp_ctx);
1769 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1771 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1772 talloc_free(tmp_ctx);
1776 /* update the recovery daemon so it now knows to expect the new
1777 node assignment for this ip.
1779 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1781 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1785 talloc_free(tmp_ctx);
1791 * scans all other nodes and returns a pnn for another node that can host this
1795 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1797 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1798 struct ctdb_all_public_ips *ips;
1799 struct ctdb_node_map *nodemap=NULL;
1802 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1804 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1805 talloc_free(tmp_ctx);
1809 for(i=0;i<nodemap->num;i++){
1810 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1813 if (nodemap->nodes[i].pnn == options.pnn) {
1817 /* read the public ip list from this node */
1818 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1820 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1824 for (j=0;j<ips->num;j++) {
1825 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1826 talloc_free(tmp_ctx);
1827 return nodemap->nodes[i].pnn;
1833 talloc_free(tmp_ctx);
1837 /* If pnn is -1 then try to find a node to move IP to... */
1838 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1840 bool pnn_specified = (pnn == -1 ? false : true);
1843 while (retries < 5) {
1844 if (!pnn_specified) {
1845 pnn = find_other_host_for_public_ip(ctdb, addr);
1850 ("Trying to move public IP to node %u\n", pnn));
1853 if (move_ip(ctdb, addr, pnn) == 0) {
1866 move/failover an ip address to a specific node
1868 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1871 ctdb_sock_addr addr;
1873 assert_single_node_only();
1880 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1881 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1886 if (sscanf(argv[1], "%u", &pnn) != 1) {
1887 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1891 if (!try_moveip(ctdb, &addr, pnn)) {
1892 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1899 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1903 data.dptr = (uint8_t *)&pnn;
1904 data.dsize = sizeof(uint32_t);
1905 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1907 ("Failed to send message to force node %u to be a rebalancing target\n",
1917 rebalance a node by setting it to allow failback and triggering a
1920 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1922 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1927 assert_single_node_only();
1933 /* Determine the nodes where IPs need to be reloaded */
1934 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1935 options.pnn, true, &nodes, &pnn_mode)) {
1940 for (i = 0; i < talloc_array_length(nodes); i++) {
1941 if (!rebalance_node(ctdb, nodes[i])) {
1947 talloc_free(tmp_ctx);
1951 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1953 struct ctdb_public_ip ip;
1956 uint32_t disable_time;
1958 struct ctdb_node_map *nodemap=NULL;
1959 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1962 data.dptr = (uint8_t*)&disable_time;
1963 data.dsize = sizeof(disable_time);
1964 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1966 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1973 data.dptr = (uint8_t *)&ip;
1974 data.dsize = sizeof(ip);
1976 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1978 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1979 talloc_free(tmp_ctx);
1983 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1984 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1991 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1992 talloc_free(tmp_ctx);
1996 talloc_free(tmp_ctx);
2001 release an ip form all nodes and have it re-assigned by recd
2003 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2005 ctdb_sock_addr addr;
2007 assert_single_node_only();
2014 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2015 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2019 if (rebalance_ip(ctdb, &addr) != 0) {
2020 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2027 static int getips_store_callback(void *param, void *data)
2029 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2030 struct ctdb_all_public_ips *ips = param;
2034 ips->ips[i].pnn = node_ip->pnn;
2035 ips->ips[i].addr = node_ip->addr;
2039 static int getips_count_callback(void *param, void *data)
2041 uint32_t *count = param;
2048 static uint32_t *ip_key(ctdb_sock_addr *ip)
2050 static uint32_t key[IP_KEYLEN];
2052 bzero(key, sizeof(key));
2054 switch (ip->sa.sa_family) {
2056 key[0] = ip->ip.sin_addr.s_addr;
2059 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2067 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2074 static void *add_ip_callback(void *parm, void *data)
2080 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2082 struct ctdb_all_public_ips *tmp_ips;
2083 struct ctdb_node_map *nodemap=NULL;
2084 trbt_tree_t *ip_tree;
2088 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2090 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2094 ip_tree = trbt_create(tmp_ctx, 0);
2096 for(i=0;i<nodemap->num;i++){
2097 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2100 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2104 /* read the public ip list from this node */
2105 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2107 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2111 for (j=0; j<tmp_ips->num;j++) {
2112 struct ctdb_public_ip *node_ip;
2114 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2115 node_ip->pnn = tmp_ips->ips[j].pnn;
2116 node_ip->addr = tmp_ips->ips[j].addr;
2118 trbt_insertarray32_callback(ip_tree,
2119 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2123 talloc_free(tmp_ips);
2128 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2130 len = offsetof(struct ctdb_all_public_ips, ips) +
2131 count*sizeof(struct ctdb_public_ip);
2132 tmp_ips = talloc_zero_size(tmp_ctx, len);
2133 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2141 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2143 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2145 event_add_timed(ctdb->ev, ctdb,
2146 timeval_current_ofs(1, 0),
2147 ctdb_every_second, ctdb);
2150 struct srvid_reply_handler_data {
2154 const char *srvid_str;
2157 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2162 struct srvid_reply_handler_data *d =
2163 (struct srvid_reply_handler_data *)private_data;
2167 if (data.dsize != sizeof(ret)) {
2168 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2172 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2173 ret = *(int32_t *)data.dptr;
2176 ("%s failed with result %d\n", d->srvid_str, ret));
2180 if (!d->wait_for_all) {
2185 /* Wait for all replies */
2187 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2188 if (d->nodes[i] == ret) {
2190 ("%s reply received from node %u\n",
2191 d->srvid_str, ret));
2194 if (d->nodes[i] != -1) {
2195 /* Found a node that hasn't yet replied */
2201 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2202 * or replies from all connected nodes. arg is the data argument to
2203 * pass in the srvid_request structure - pass 0 if this isn't needed.
2205 static int srvid_broadcast(struct ctdb_context *ctdb,
2206 uint64_t srvid, uint32_t *arg,
2207 const char *srvid_str, bool wait_for_all)
2212 uint64_t reply_srvid;
2213 struct srvid_request request;
2214 struct srvid_request_data request_data;
2215 struct srvid_reply_handler_data reply_data;
2218 ZERO_STRUCT(request);
2220 /* Time ticks to enable timeouts to be processed */
2221 event_add_timed(ctdb->ev, ctdb,
2222 timeval_current_ofs(1, 0),
2223 ctdb_every_second, ctdb);
2225 pnn = ctdb_get_pnn(ctdb);
2226 reply_srvid = getpid();
2230 request.srvid = reply_srvid;
2232 data.dptr = (uint8_t *)&request;
2233 data.dsize = sizeof(request);
2235 request_data.pnn = pnn;
2236 request_data.srvid = reply_srvid;
2237 request_data.data = *arg;
2239 data.dptr = (uint8_t *)&request_data;
2240 data.dsize = sizeof(request_data);
2243 /* Register message port for reply from recovery master */
2244 ctdb_client_set_message_handler(ctdb, reply_srvid,
2245 srvid_broadcast_reply_handler,
2248 reply_data.wait_for_all = wait_for_all;
2249 reply_data.nodes = NULL;
2250 reply_data.srvid_str = srvid_str;
2253 reply_data.done = false;
2256 struct ctdb_node_map *nodemap;
2258 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2259 CTDB_CURRENT_NODE, ctdb, &nodemap);
2262 ("Unable to get nodemap from current node, try again\n"));
2267 if (reply_data.nodes != NULL) {
2268 talloc_free(reply_data.nodes);
2270 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2273 talloc_free(nodemap);
2276 /* Send to all connected nodes. Only recmaster replies */
2277 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2280 /* This can only happen if the socket is closed and
2281 * there's no way to recover from that, so don't try
2285 ("Failed to send %s request to connected nodes\n",
2290 tv = timeval_current();
2291 /* This loop terminates the reply is received */
2292 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2293 event_loop_once(ctdb->ev);
2296 if (!reply_data.done) {
2298 ("Still waiting for confirmation of %s\n", srvid_str));
2303 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2305 talloc_free(reply_data.nodes);
2310 static int ipreallocate(struct ctdb_context *ctdb)
2312 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2313 "IP reallocation", false);
2317 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2319 return ipreallocate(ctdb);
2323 add a public ip address to a node
2325 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2328 int len, retries = 0;
2330 ctdb_sock_addr addr;
2331 struct ctdb_control_ip_iface *pub;
2332 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2333 struct ctdb_all_public_ips *ips;
2337 talloc_free(tmp_ctx);
2341 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2342 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2343 talloc_free(tmp_ctx);
2347 /* read the public ip list from the node */
2348 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2350 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2351 talloc_free(tmp_ctx);
2354 for (i=0;i<ips->num;i++) {
2355 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2356 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2363 /* Dont timeout. This command waits for an ip reallocation
2364 which sometimes can take wuite a while if there has
2365 been a recent recovery
2369 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2370 pub = talloc_size(tmp_ctx, len);
2371 CTDB_NO_MEMORY(ctdb, pub);
2375 pub->len = strlen(argv[1])+1;
2376 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2379 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2381 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2385 } while (retries < 5 && ret != 0);
2387 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2388 talloc_free(tmp_ctx);
2392 if (rebalance_node(ctdb, options.pnn) != 0) {
2393 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2397 talloc_free(tmp_ctx);
2402 add a public ip address to a node
2404 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2406 ctdb_sock_addr addr;
2412 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2413 printf("Badly formed ip : %s\n", argv[0]);
2417 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2422 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2424 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2426 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2427 struct ctdb_node_map *nodemap=NULL;
2428 struct ctdb_all_public_ips *ips;
2431 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2433 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2437 /* remove it from the nodes that are not hosting the ip currently */
2438 for(i=0;i<nodemap->num;i++){
2439 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2442 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2444 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2448 for (j=0;j<ips->num;j++) {
2449 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2457 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2461 options.pnn = nodemap->nodes[i].pnn;
2462 control_delip(ctdb, argc, argv);
2466 /* remove it from every node (also the one hosting it) */
2467 for(i=0;i<nodemap->num;i++){
2468 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2471 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2473 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2477 for (j=0;j<ips->num;j++) {
2478 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2486 options.pnn = nodemap->nodes[i].pnn;
2487 control_delip(ctdb, argc, argv);
2490 talloc_free(tmp_ctx);
2495 delete a public ip address from a node
2497 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2500 ctdb_sock_addr addr;
2501 struct ctdb_control_ip_iface pub;
2502 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2503 struct ctdb_all_public_ips *ips;
2506 talloc_free(tmp_ctx);
2510 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2511 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2515 if (options.pnn == CTDB_BROADCAST_ALL) {
2516 return control_delip_all(ctdb, argc, argv, &addr);
2523 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2525 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2526 talloc_free(tmp_ctx);
2530 for (i=0;i<ips->num;i++) {
2531 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2537 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2538 ctdb_addr_to_str(&addr)));
2539 talloc_free(tmp_ctx);
2543 /* This is an optimisation. If this node is hosting the IP
2544 * then try to move it somewhere else without invoking a full
2545 * takeover run. We don't care if this doesn't work!
2547 if (ips->ips[i].pnn == options.pnn) {
2548 (void) try_moveip(ctdb, &addr, -1);
2551 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2553 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2554 talloc_free(tmp_ctx);
2558 talloc_free(tmp_ctx);
2562 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2563 int argc, const char **argv)
2565 struct ctdb_control_killtcp *killtcp;
2566 int max_entries, current, i;
2567 struct timeval timeout;
2568 char line[128], src[128], dst[128];
2571 struct client_async_data *async_data;
2572 struct ctdb_client_control_state *state;
2582 while (!feof(stdin)) {
2583 if (fgets(line, sizeof(line), stdin) == NULL) {
2587 /* Silently skip empty lines */
2588 if (line[0] == '\n') {
2592 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2593 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2595 talloc_free(killtcp);
2599 if (current >= max_entries) {
2600 max_entries += 1024;
2601 killtcp = talloc_realloc(ctdb, killtcp,
2602 struct ctdb_control_killtcp,
2604 CTDB_NO_MEMORY(ctdb, killtcp);
2607 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2608 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2610 talloc_free(killtcp);
2614 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2615 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2617 talloc_free(killtcp);
2624 async_data = talloc_zero(ctdb, struct client_async_data);
2625 if (async_data == NULL) {
2626 talloc_free(killtcp);
2630 for (i = 0; i < current; i++) {
2632 data.dsize = sizeof(struct ctdb_control_killtcp);
2633 data.dptr = (unsigned char *)&killtcp[i];
2635 timeout = TIMELIMIT();
2636 state = ctdb_control_send(ctdb, options.pnn, 0,
2637 CTDB_CONTROL_KILL_TCP, 0, data,
2638 async_data, &timeout, NULL);
2640 if (state == NULL) {
2642 ("Failed to call async killtcp control to node %u\n",
2644 talloc_free(killtcp);
2648 ctdb_client_async_add(async_data, state);
2651 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2652 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2653 talloc_free(killtcp);
2657 talloc_free(killtcp);
2663 kill a tcp connection
2665 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2668 struct ctdb_control_killtcp killtcp;
2670 assert_single_node_only();
2673 return kill_tcp_from_file(ctdb, argc, argv);
2680 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2681 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2685 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2686 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2690 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2692 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2703 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2706 ctdb_sock_addr addr;
2708 assert_single_node_only();
2714 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2715 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2719 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2721 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2729 register a server id
2731 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2734 struct ctdb_server_id server_id;
2740 server_id.pnn = strtoul(argv[0], NULL, 0);
2741 server_id.type = strtoul(argv[1], NULL, 0);
2742 server_id.server_id = strtoul(argv[2], NULL, 0);
2744 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2746 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2749 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2755 unregister a server id
2757 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2760 struct ctdb_server_id server_id;
2766 server_id.pnn = strtoul(argv[0], NULL, 0);
2767 server_id.type = strtoul(argv[1], NULL, 0);
2768 server_id.server_id = strtoul(argv[2], NULL, 0);
2770 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2772 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2779 check if a server id exists
2781 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2785 struct ctdb_server_id server_id;
2791 server_id.pnn = strtoul(argv[0], NULL, 0);
2792 server_id.type = strtoul(argv[1], NULL, 0);
2793 server_id.server_id = strtoul(argv[2], NULL, 0);
2795 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2797 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2802 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2804 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2810 get a list of all server ids that are registered on a node
2812 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2815 struct ctdb_server_id_list *server_ids;
2817 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2819 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2823 for (i=0; i<server_ids->num; i++) {
2824 printf("Server id %d:%d:%d\n",
2825 server_ids->server_ids[i].pnn,
2826 server_ids->server_ids[i].type,
2827 server_ids->server_ids[i].server_id);
2834 check if a server id exists
2836 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2838 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2844 talloc_free(tmp_ctx);
2848 ids = talloc_array(tmp_ctx, uint64_t, argc);
2849 result = talloc_array(tmp_ctx, uint8_t, argc);
2851 for (i = 0; i < argc; i++) {
2852 ids[i] = strtoull(argv[i], NULL, 0);
2855 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2856 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2858 talloc_free(tmp_ctx);
2862 for (i=0; i < argc; i++) {
2863 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2864 result[i] ? "exists" : "does not exist");
2867 talloc_free(tmp_ctx);
2872 send a tcp tickle ack
2874 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2877 ctdb_sock_addr src, dst;
2883 if (!parse_ip_port(argv[0], &src)) {
2884 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2888 if (!parse_ip_port(argv[1], &dst)) {
2889 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2893 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2897 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2904 display public ip status
2906 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2909 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2910 struct ctdb_all_public_ips *ips;
2912 if (options.pnn == CTDB_BROADCAST_ALL) {
2913 /* read the list of public ips from all nodes */
2914 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2916 /* read the public ip list from this node */
2917 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2920 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2921 talloc_free(tmp_ctx);
2925 if (options.machinereadable){
2926 printf(":Public IP:Node:");
2927 if (options.verbose){
2928 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2932 if (options.pnn == CTDB_BROADCAST_ALL) {
2933 printf("Public IPs on ALL nodes\n");
2935 printf("Public IPs on node %u\n", options.pnn);
2939 for (i=1;i<=ips->num;i++) {
2940 struct ctdb_control_public_ip_info *info = NULL;
2942 char *aciface = NULL;
2943 char *avifaces = NULL;
2944 char *cifaces = NULL;
2946 if (options.pnn == CTDB_BROADCAST_ALL) {
2947 pnn = ips->ips[ips->num-i].pnn;
2953 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2954 &ips->ips[ips->num-i].addr, &info);
2961 for (j=0; j < info->num; j++) {
2962 if (cifaces == NULL) {
2963 cifaces = talloc_strdup(info,
2964 info->ifaces[j].name);
2966 cifaces = talloc_asprintf_append(cifaces,
2968 info->ifaces[j].name);
2971 if (info->active_idx == j) {
2972 aciface = info->ifaces[j].name;
2975 if (info->ifaces[j].link_state == 0) {
2979 if (avifaces == NULL) {
2980 avifaces = talloc_strdup(info, info->ifaces[j].name);
2982 avifaces = talloc_asprintf_append(avifaces,
2984 info->ifaces[j].name);
2989 if (options.machinereadable){
2991 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2992 ips->ips[ips->num-i].pnn);
2993 if (options.verbose){
2996 avifaces?avifaces:"",
2997 cifaces?cifaces:"");
3001 if (options.verbose) {
3002 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3003 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3004 ips->ips[ips->num-i].pnn,
3006 avifaces?avifaces:"",
3007 cifaces?cifaces:"");
3010 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3011 ips->ips[ips->num-i].pnn);
3017 talloc_free(tmp_ctx);
3024 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3027 ctdb_sock_addr addr;
3028 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3029 struct ctdb_control_public_ip_info *info;
3032 talloc_free(tmp_ctx);
3036 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3037 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3041 /* read the public ip info from this node */
3042 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3043 tmp_ctx, &addr, &info);
3045 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3046 argv[0], options.pnn));
3047 talloc_free(tmp_ctx);
3051 printf("Public IP[%s] info on node %u\n",
3052 ctdb_addr_to_str(&info->ip.addr),
3055 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3056 ctdb_addr_to_str(&info->ip.addr),
3057 info->ip.pnn, info->num);
3059 for (i=0; i<info->num; i++) {
3060 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3062 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3063 i+1, info->ifaces[i].name,
3064 info->ifaces[i].link_state?"up":"down",
3065 (unsigned int)info->ifaces[i].references,
3066 (i==info->active_idx)?" (active)":"");
3069 talloc_free(tmp_ctx);
3074 display interfaces status
3076 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3078 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3080 struct ctdb_control_get_ifaces *ifaces;
3083 /* read the public ip list from this node */
3084 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3086 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3088 talloc_free(tmp_ctx);
3092 if (options.machinereadable){
3093 printf(":Name:LinkStatus:References:\n");
3095 printf("Interfaces on node %u\n", options.pnn);
3098 for (i=0; i<ifaces->num; i++) {
3099 if (options.machinereadable){
3100 printf(":%s:%s:%u\n",
3101 ifaces->ifaces[i].name,
3102 ifaces->ifaces[i].link_state?"1":"0",
3103 (unsigned int)ifaces->ifaces[i].references);
3105 printf("name:%s link:%s references:%u\n",
3106 ifaces->ifaces[i].name,
3107 ifaces->ifaces[i].link_state?"up":"down",
3108 (unsigned int)ifaces->ifaces[i].references);
3112 talloc_free(tmp_ctx);
3118 set link status of an interface
3120 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3123 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3124 struct ctdb_control_iface_info info;
3132 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3133 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3135 talloc_free(tmp_ctx);
3138 strcpy(info.name, argv[0]);
3140 if (strcmp(argv[1], "up") == 0) {
3141 info.link_state = 1;
3142 } else if (strcmp(argv[1], "down") == 0) {
3143 info.link_state = 0;
3145 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3147 talloc_free(tmp_ctx);
3151 /* read the public ip list from this node */
3152 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3155 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3156 argv[0], options.pnn));
3157 talloc_free(tmp_ctx);
3161 talloc_free(tmp_ctx);
3166 display pid of a ctdb daemon
3168 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3173 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3175 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3178 printf("Pid:%d\n", pid);
3183 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3185 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3187 update_flags_handler_t handler,
3192 struct ctdb_node_map *nodemap = NULL;
3196 /* Check if the node is already in the desired state */
3197 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3199 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3202 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3203 if (set_flag == flag_is_set) {
3204 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3205 (set_flag ? "already" : "not"), desc));
3210 if (!handler(ctdb, data)) {
3211 DEBUG(DEBUG_WARNING,
3212 ("Failed to send control to set state %s on node %u, try again\n",
3213 desc, options.pnn));
3218 /* Read the nodemap and verify the change took effect.
3219 * Even if the above control/hanlder timed out then it
3220 * could still have worked!
3222 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3225 DEBUG(DEBUG_WARNING,
3226 ("Unable to get nodemap from local node, try again\n"));
3228 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3229 } while (nodemap == NULL || (set_flag != flag_is_set));
3231 return ipreallocate(ctdb);
3234 /* Administratively disable a node */
3235 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3239 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3240 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3244 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3246 return update_flags_and_ipreallocate(ctdb, NULL,
3247 update_flags_disabled,
3248 NODE_FLAGS_PERMANENTLY_DISABLED,
3250 true /* set_flag*/);
3253 /* Administratively re-enable a node */
3254 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3258 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3259 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3263 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3265 return update_flags_and_ipreallocate(ctdb, NULL,
3266 update_flags_not_disabled,
3267 NODE_FLAGS_PERMANENTLY_DISABLED,
3269 false /* set_flag*/);
3273 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3277 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3282 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3284 return update_flags_and_ipreallocate(ctdb, NULL,
3285 update_flags_stopped,
3288 true /* set_flag*/);
3291 /* Continue a stopped node */
3292 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3296 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3301 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3303 return update_flags_and_ipreallocate(ctdb, NULL,
3304 update_flags_not_stopped,
3307 false /* set_flag */);
3310 static uint32_t get_generation(struct ctdb_context *ctdb)
3312 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3313 struct ctdb_vnn_map *vnnmap=NULL;
3315 uint32_t generation;
3317 /* wait until the recmaster is not in recovery mode */
3319 uint32_t recmode, recmaster;
3321 if (vnnmap != NULL) {
3322 talloc_free(vnnmap);
3326 /* get the recmaster */
3327 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3329 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3330 talloc_free(tmp_ctx);
3334 /* get recovery mode */
3335 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3337 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3338 talloc_free(tmp_ctx);
3342 /* get the current generation number */
3343 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3345 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3346 talloc_free(tmp_ctx);
3350 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3351 generation = vnnmap->generation;
3352 talloc_free(tmp_ctx);
3360 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3362 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3365 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3370 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3372 struct ctdb_ban_time bantime;
3378 bantime.pnn = options.pnn;
3379 bantime.time = strtoul(argv[0], NULL, 0);
3381 if (bantime.time == 0) {
3382 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3386 return update_flags_and_ipreallocate(ctdb, &bantime,
3387 update_state_banned,
3390 true /* set_flag*/);
3395 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3397 struct ctdb_ban_time bantime;
3399 bantime.pnn = options.pnn;
3402 return update_flags_and_ipreallocate(ctdb, &bantime,
3403 update_state_banned,
3406 false /* set_flag*/);
3410 show ban information for a node
3412 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3415 struct ctdb_node_map *nodemap=NULL;
3416 struct ctdb_ban_time *bantime;
3418 /* verify the node exists */
3419 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3421 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3425 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3427 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3431 if (bantime->time == 0) {
3432 printf("Node %u is not banned\n", bantime->pnn);
3434 printf("Node %u is banned, %d seconds remaining\n",
3435 bantime->pnn, bantime->time);
3444 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3448 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3450 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3460 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3463 uint32_t generation, next_generation;
3466 /* "force" option ignores freeze failure and forces recovery */
3467 force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3469 /* record the current generation number */
3470 generation = get_generation(ctdb);
3472 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3475 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3478 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3481 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3483 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3487 /* wait until we are in a new generation */
3489 next_generation = get_generation(ctdb);
3490 if (next_generation != generation) {
3501 display monitoring mode of a remote node
3503 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3508 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3510 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3513 if (!options.machinereadable){
3514 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3517 printf(":%d:\n",monmode);
3524 display capabilities of a remote node
3526 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3528 uint32_t capabilities;
3531 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3533 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3537 if (!options.machinereadable){
3538 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3539 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3540 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3541 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3543 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3544 printf(":%d:%d:%d:%d:\n",
3545 !!(capabilities&CTDB_CAP_RECMASTER),
3546 !!(capabilities&CTDB_CAP_LMASTER),
3547 !!(capabilities&CTDB_CAP_LVS),
3548 !!(capabilities&CTDB_CAP_NATGW));
3554 display lvs configuration
3557 static uint32_t lvs_exclude_flags[] = {
3558 /* Look for a nice healthy node */
3559 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3560 /* If not found, an UNHEALTHY node will do */
3561 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3565 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3567 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3568 struct ctdb_node_map *orig_nodemap=NULL;
3569 struct ctdb_node_map *nodemap;
3572 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3573 tmp_ctx, &orig_nodemap);
3575 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3576 talloc_free(tmp_ctx);
3580 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3582 if (nodemap == NULL) {
3590 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3591 struct ctdb_node_map *t =
3592 filter_nodemap_by_flags(ctdb, nodemap,
3593 lvs_exclude_flags[i]);
3600 /* At least 1 node without excluded flags */
3602 for (j = 0; j < t->num; j++) {
3603 printf("%d:%s\n", t->nodes[j].pnn,
3604 ctdb_addr_to_str(&t->nodes[j].addr));
3611 talloc_free(tmp_ctx);
3616 display who is the lvs master
3618 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3620 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3621 struct ctdb_node_map *orig_nodemap=NULL;
3622 struct ctdb_node_map *nodemap;
3625 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3626 tmp_ctx, &orig_nodemap);
3628 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3629 talloc_free(tmp_ctx);
3633 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3635 if (nodemap == NULL) {
3641 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3642 struct ctdb_node_map *t =
3643 filter_nodemap_by_flags(ctdb, nodemap,
3644 lvs_exclude_flags[i]);
3652 printf(options.machinereadable ?
3653 "%d\n" : "Node %d is LVS master\n",
3660 printf("There is no LVS master\n");
3663 talloc_free(tmp_ctx);
3668 disable monitoring on a node
3670 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3675 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3677 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3680 printf("Monitoring mode:%s\n","DISABLED");
3686 enable monitoring on a node
3688 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3693 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3695 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3698 printf("Monitoring mode:%s\n","ACTIVE");
3704 display remote list of keys/data for a db
3706 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3708 const char *db_name;
3709 struct ctdb_db_context *ctdb_db;
3711 struct ctdb_dump_db_context c;
3718 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3722 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3723 if (ctdb_db == NULL) {
3724 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3728 if (options.printlmaster) {
3729 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3730 ctdb, &ctdb->vnn_map);
3732 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3740 c.printemptyrecords = (bool)options.printemptyrecords;
3741 c.printdatasize = (bool)options.printdatasize;
3742 c.printlmaster = (bool)options.printlmaster;
3743 c.printhash = (bool)options.printhash;
3744 c.printrecordflags = (bool)options.printrecordflags;
3746 /* traverse and dump the cluster tdb */
3747 ret = ctdb_dump_db(ctdb_db, &c);
3749 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3750 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3751 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3755 talloc_free(ctdb_db);
3757 printf("Dumped %d records\n", ret);
3761 struct cattdb_data {
3762 struct ctdb_context *ctdb;
3766 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3768 struct cattdb_data *d = private_data;
3769 struct ctdb_dump_db_context c;
3775 c.printemptyrecords = (bool)options.printemptyrecords;
3776 c.printdatasize = (bool)options.printdatasize;
3777 c.printlmaster = false;
3778 c.printhash = (bool)options.printhash;
3779 c.printrecordflags = true;
3781 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3785 cat the local tdb database using same format as catdb
3787 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3789 const char *db_name;
3790 struct ctdb_db_context *ctdb_db;
3791 struct cattdb_data d;
3798 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3802 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3803 if (ctdb_db == NULL) {
3804 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3808 /* traverse the local tdb */
3811 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3812 printf("Failed to cattdb data\n");
3815 talloc_free(ctdb_db);
3817 printf("Dumped %d records\n", d.count);
3822 display the content of a database key
3824 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3826 const char *db_name;
3827 struct ctdb_db_context *ctdb_db;
3828 struct ctdb_record_handle *h;
3829 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3837 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3841 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3842 if (ctdb_db == NULL) {
3843 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3847 key.dptr = discard_const(argv[1]);
3848 key.dsize = strlen((char *)key.dptr);
3850 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3852 printf("Failed to fetch record '%s' on node %d\n",
3853 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3854 talloc_free(tmp_ctx);
3858 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3860 talloc_free(ctdb_db);
3861 talloc_free(tmp_ctx);
3866 display the content of a database key
3868 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3870 const char *db_name;
3871 struct ctdb_db_context *ctdb_db;
3872 struct ctdb_record_handle *h;
3873 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3881 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3885 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3886 if (ctdb_db == NULL) {
3887 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3891 key.dptr = discard_const(argv[1]);
3892 key.dsize = strlen((char *)key.dptr);
3894 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3896 printf("Failed to fetch record '%s' on node %d\n",
3897 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3898 talloc_free(tmp_ctx);
3902 data.dptr = discard_const(argv[2]);
3903 data.dsize = strlen((char *)data.dptr);
3905 if (ctdb_record_store(h, data) != 0) {
3906 printf("Failed to store record\n");
3910 talloc_free(ctdb_db);
3911 talloc_free(tmp_ctx);
3916 fetch a record from a persistent database
3918 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3920 const char *db_name;
3921 struct ctdb_db_context *ctdb_db;
3922 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3923 struct ctdb_transaction_handle *h;
3930 talloc_free(tmp_ctx);
3934 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3935 talloc_free(tmp_ctx);
3939 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3941 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3942 talloc_free(tmp_ctx);
3946 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3947 if (ctdb_db == NULL) {
3948 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3949 talloc_free(tmp_ctx);
3953 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3955 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3956 talloc_free(tmp_ctx);
3960 key.dptr = discard_const(argv[1]);
3961 key.dsize = strlen(argv[1]);
3962 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3964 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3965 talloc_free(tmp_ctx);
3969 if (data.dsize == 0 || data.dptr == NULL) {
3970 DEBUG(DEBUG_ERR,("Record is empty\n"));
3971 talloc_free(tmp_ctx);
3976 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3978 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3979 talloc_free(tmp_ctx);
3982 write(fd, data.dptr, data.dsize);
3985 write(1, data.dptr, data.dsize);
3988 /* abort the transaction */
3992 talloc_free(tmp_ctx);
3997 fetch a record from a tdb-file
3999 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4001 const char *tdb_file;
4004 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4013 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4015 printf("Failed to open TDB file %s\n", tdb_file);
4019 if (!strncmp(argv[1], "0x", 2)) {
4020 key = hextodata(tmp_ctx, argv[1] + 2);
4021 if (key.dsize == 0) {
4022 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4026 key.dptr = discard_const(argv[1]);
4027 key.dsize = strlen(argv[1]);
4030 data = tdb_fetch(tdb, key);
4031 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4032 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4040 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4042 printf("Failed to open output file %s\n", argv[2]);
4045 if (options.verbose){
4046 write(fd, data.dptr, data.dsize);
4048 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4052 if (options.verbose){
4053 write(1, data.dptr, data.dsize);
4055 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4059 talloc_free(tmp_ctx);
4064 store a record and header to a tdb-file
4066 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4068 const char *tdb_file;
4070 TDB_DATA key, value, data;
4071 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4072 struct ctdb_ltdb_header header;
4080 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4082 printf("Failed to open TDB file %s\n", tdb_file);
4086 if (!strncmp(argv[1], "0x", 2)) {
4087 key = hextodata(tmp_ctx, argv[1] + 2);
4088 if (key.dsize == 0) {
4089 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4093 key.dptr = discard_const(argv[1]);
4094 key.dsize = strlen(argv[1]);
4097 if (!strncmp(argv[2], "0x", 2)) {
4098 value = hextodata(tmp_ctx, argv[2] + 2);
4099 if (value.dsize == 0) {
4100 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4104 value.dptr = discard_const(argv[2]);
4105 value.dsize = strlen(argv[2]);
4108 ZERO_STRUCT(header);
4110 header.rsn = atoll(argv[3]);
4113 header.dmaster = atoi(argv[4]);
4116 header.flags = atoi(argv[5]);
4119 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4120 data.dptr = talloc_size(tmp_ctx, data.dsize);
4121 if (data.dptr == NULL) {
4122 printf("Failed to allocate header+value\n");
4126 *(struct ctdb_ltdb_header *)data.dptr = header;
4127 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4129 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4130 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4137 talloc_free(tmp_ctx);
4142 write a record to a persistent database
4144 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4146 const char *db_name;
4147 struct ctdb_db_context *ctdb_db;
4148 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4149 struct ctdb_transaction_handle *h;
4155 talloc_free(tmp_ctx);
4159 fd = open(argv[2], O_RDONLY);
4161 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4162 talloc_free(tmp_ctx);
4166 ret = fstat(fd, &st);
4168 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4170 talloc_free(tmp_ctx);
4174 if (!S_ISREG(st.st_mode)) {
4175 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4177 talloc_free(tmp_ctx);
4181 data.dsize = st.st_size;
4182 if (data.dsize == 0) {
4185 data.dptr = talloc_size(tmp_ctx, data.dsize);
4186 if (data.dptr == NULL) {
4187 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4189 talloc_free(tmp_ctx);
4192 ret = read(fd, data.dptr, data.dsize);
4193 if (ret != data.dsize) {
4194 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4196 talloc_free(tmp_ctx);
4205 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4206 if (ctdb_db == NULL) {
4207 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4208 talloc_free(tmp_ctx);
4212 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4214 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4215 talloc_free(tmp_ctx);
4219 key.dptr = discard_const(argv[1]);
4220 key.dsize = strlen(argv[1]);
4221 ret = ctdb_transaction_store(h, key, data);
4223 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4224 talloc_free(tmp_ctx);
4228 ret = ctdb_transaction_commit(h);
4230 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4231 talloc_free(tmp_ctx);
4236 talloc_free(tmp_ctx);
4241 * delete a record from a persistent database
4243 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4245 const char *db_name;
4246 struct ctdb_db_context *ctdb_db;
4247 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4248 struct ctdb_transaction_handle *h;
4255 talloc_free(tmp_ctx);
4259 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4260 talloc_free(tmp_ctx);
4264 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4266 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4267 talloc_free(tmp_ctx);
4271 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4272 if (ctdb_db == NULL) {
4273 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4274 talloc_free(tmp_ctx);
4278 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4280 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4281 talloc_free(tmp_ctx);
4285 key.dptr = discard_const(argv[1]);
4286 key.dsize = strlen(argv[1]);
4287 ret = ctdb_transaction_store(h, key, tdb_null);
4289 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4290 talloc_free(tmp_ctx);
4294 ret = ctdb_transaction_commit(h);
4296 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4297 talloc_free(tmp_ctx);
4301 talloc_free(tmp_ctx);
4305 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4310 const char *ret; /* Next byte after successfully parsed value */
4312 /* Error, unless someone says otherwise */
4314 /* Indicates no value to parse */
4317 /* Skip whitespace */
4318 n = strspn(s, " \t");
4322 /* Quoted ASCII string - no wide characters! */
4324 n = strcspn(t, "\"");
4328 data->dptr = talloc_memdup(mem_ctx, t, n);
4329 CTDB_NOMEM_ABORT(data->dptr);
4333 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4336 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4342 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4343 TDB_DATA *key, TDB_DATA *value)
4345 char line [1024]; /* FIXME: make this more flexible? */
4349 ptr = fgets(line, sizeof(line), file);
4356 t = ptrans_parse_string(mem_ctx, line, key);
4357 if (t == NULL || key->dptr == NULL) {
4358 /* Line Ignored but not EOF */
4363 t = ptrans_parse_string(mem_ctx, t, value);
4365 /* Line Ignored but not EOF */
4366 talloc_free(key->dptr);
4375 * Update a persistent database as per file/stdin
4377 static int control_ptrans(struct ctdb_context *ctdb,
4378 int argc, const char **argv)
4380 const char *db_name;
4381 struct ctdb_db_context *ctdb_db;
4382 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4383 struct ctdb_transaction_handle *h;
4384 TDB_DATA key, value;
4389 talloc_free(tmp_ctx);
4395 file = fopen(argv[1], "r");
4397 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4398 talloc_free(tmp_ctx);
4405 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4406 if (ctdb_db == NULL) {
4407 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4411 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4413 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4417 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4418 if (key.dsize != 0) {
4419 ret = ctdb_transaction_store(h, key, value);
4420 /* Minimise memory use */
4421 talloc_free(key.dptr);
4422 if (value.dptr != NULL) {
4423 talloc_free(value.dptr);
4426 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4427 ctdb_transaction_cancel(h);
4433 ret = ctdb_transaction_commit(h);
4435 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4439 if (file != stdin) {
4442 talloc_free(tmp_ctx);
4446 if (file != stdin) {
4450 talloc_free(tmp_ctx);
4455 check if a service is bound to a port or not
4457 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4462 struct sockaddr_in sin;
4465 printf("Use: ctdb chktcport <port>\n");
4469 port = atoi(argv[0]);
4471 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4473 printf("Failed to open local socket\n");
4477 v = fcntl(s, F_GETFL, 0);
4478 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4479 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4482 bzero(&sin, sizeof(sin));
4483 sin.sin_family = PF_INET;
4484 sin.sin_port = htons(port);
4485 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4488 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4497 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4498 TDB_DATA data, void *private_data)
4500 DEBUG(DEBUG_ERR,("Log data received\n"));
4501 if (data.dsize > 0) {
4502 printf("%s", data.dptr);
4509 display a list of log messages from the in memory ringbuffer
4511 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4515 struct ctdb_get_log_addr log_addr;
4519 /* Process options */
4521 log_addr.pnn = ctdb_get_pnn(ctdb);
4522 log_addr.level = DEBUG_NOTICE;
4523 for (i = 0; i < argc; i++) {
4524 if (strcmp(argv[i], "recoverd") == 0) {
4525 main_daemon = false;
4527 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4528 log_addr.level = get_debug_by_desc(argv[i]);
4530 log_addr.level = strtol(argv[i], NULL, 0);
4535 /* Our message port is our PID */
4536 log_addr.srvid = getpid();
4538 data.dptr = (unsigned char *)&log_addr;
4539 data.dsize = sizeof(log_addr);
4541 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4543 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4546 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4551 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4553 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4554 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4555 if (ret != 0 || res != 0) {
4556 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4557 talloc_free(tmp_ctx);
4560 talloc_free(tmp_ctx);
4562 ret = ctdb_client_send_message(ctdb, options.pnn,
4563 CTDB_SRVID_GETLOG, data);
4565 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4570 tv = timeval_current();
4571 /* this loop will terminate when we have received the reply */
4572 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4573 event_loop_once(ctdb->ev);
4576 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4582 clear the in memory log area
4584 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4588 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4589 /* "recoverd" not given - get logs from main daemon */
4592 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4594 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4595 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4596 if (ret != 0 || res != 0) {
4597 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4598 talloc_free(tmp_ctx);
4602 talloc_free(tmp_ctx);
4604 TDB_DATA data; /* unused in recoverd... */
4607 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4609 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4617 /* Reload public IPs on a specified nodes */
4618 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4620 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4626 assert_single_node_only();
4632 /* Determine the nodes where IPs need to be reloaded */
4633 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4634 options.pnn, true, &nodes, &pnn_mode)) {
4640 /* Disable takeover runs on all connected nodes. A reply
4641 * indicating success is needed from each node so all nodes
4642 * will need to be active. This will retry until maxruntime
4643 * is exceeded, hence no error handling.
4645 * A check could be added to not allow reloading of IPs when
4646 * there are disconnected nodes. However, this should
4647 * probably be left up to the administrator.
4649 timeout = LONGTIMEOUT;
4650 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4651 "Disable takeover runs", true);
4653 /* Now tell all the desired nodes to reload their public IPs.
4654 * Keep trying this until it succeeds. This assumes all
4655 * failures are transient, which might not be true...
4657 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4658 nodes, 0, LONGTIMELIMIT(),
4660 NULL, NULL, NULL) != 0) {
4662 ("Unable to reload IPs on some nodes, try again.\n"));
4666 /* It isn't strictly necessary to wait until takeover runs are
4667 * re-enabled but doing so can't hurt.
4670 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4671 "Enable takeover runs", true);
4677 talloc_free(tmp_ctx);
4682 display a list of the databases on a remote ctdb
4684 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4687 struct ctdb_dbid_map *dbmap=NULL;
4689 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4691 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4695 if(options.machinereadable){
4696 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4697 for(i=0;i<dbmap->num;i++){
4705 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4706 dbmap->dbs[i].dbid, ctdb, &path);
4707 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4708 dbmap->dbs[i].dbid, ctdb, &name);
4709 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4710 dbmap->dbs[i].dbid, ctdb, &health);
4711 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4712 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4713 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4714 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4715 dbmap->dbs[i].dbid, name, path,
4716 !!(persistent), !!(sticky),
4717 !!(health), !!(readonly));
4722 printf("Number of databases:%d\n", dbmap->num);
4723 for(i=0;i<dbmap->num;i++){
4731 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4732 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4733 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4734 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4735 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4736 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4737 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4738 dbmap->dbs[i].dbid, name, path,
4739 persistent?" PERSISTENT":"",
4740 sticky?" STICKY":"",
4741 readonly?" READONLY":"",
4742 health?" UNHEALTHY":"");
4749 display the status of a database on a remote ctdb
4751 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4753 const char *db_name;
4763 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4767 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4768 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4769 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4770 db_id, db_name, path,
4771 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4772 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4773 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4774 (health ? health : "OK"));
4780 check if the local node is recmaster or not
4781 it will return 1 if this node is the recmaster and 0 if it is not
4782 or if the local ctdb daemon could not be contacted
4784 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4786 uint32_t mypnn, recmaster;
4789 assert_single_node_only();
4791 mypnn = getpnn(ctdb);
4793 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4795 printf("Failed to get the recmaster\n");
4799 if (recmaster != mypnn) {
4800 printf("this node is not the recmaster\n");
4804 printf("this node is the recmaster\n");
4811 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4814 struct timeval tv = timeval_current();
4815 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4817 printf("Unable to get ping response from node %u\n", options.pnn);
4820 printf("response from %u time=%.6f sec (%d clients)\n",
4821 options.pnn, timeval_elapsed(&tv), ret);
4828 get a node's runstate
4830 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4833 enum ctdb_runstate runstate;
4835 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4837 printf("Unable to get runstate response from node %u\n",
4842 enum ctdb_runstate t;
4844 for (i=0; i<argc; i++) {
4846 t = runstate_from_string(argv[i]);
4847 if (t == CTDB_RUNSTATE_UNKNOWN) {
4848 printf("Invalid run state (%s)\n", argv[i]);
4852 if (t == runstate) {
4859 printf("CTDB not in required run state (got %s)\n",
4860 runstate_to_string((enum ctdb_runstate)runstate));
4865 printf("%s\n", runstate_to_string(runstate));
4873 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4884 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4886 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4890 printf("%-23s = %u\n", name, value);
4897 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4908 value = strtoul(argv[1], NULL, 0);
4910 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4912 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4921 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4927 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4929 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4933 for (i=0;i<count;i++) {
4934 control_getvar(ctdb, 1, &list[i]);
4943 display debug level on a node
4945 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4950 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4952 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4955 if (options.machinereadable){
4956 printf(":Name:Level:\n");
4957 printf(":%s:%d:\n",get_debug_by_level(level),level);
4959 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4966 display reclock file of a node
4968 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4971 const char *reclock;
4973 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4975 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4978 if (options.machinereadable){
4979 if (reclock != NULL) {
4980 printf("%s", reclock);
4983 if (reclock == NULL) {
4984 printf("No reclock file used.\n");
4986 printf("Reclock file:%s\n", reclock);
4994 set the reclock file of a node
4996 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4999 const char *reclock;
5003 } else if (argc == 1) {
5009 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
5011 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
5018 set the natgw state on/off
5020 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
5023 uint32_t natgwstate;
5029 if (!strcmp(argv[0], "on")) {
5031 } else if (!strcmp(argv[0], "off")) {
5037 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
5039 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
5047 set the lmaster role on/off
5049 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5052 uint32_t lmasterrole;
5058 if (!strcmp(argv[0], "on")) {
5060 } else if (!strcmp(argv[0], "off")) {
5066 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
5068 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
5076 set the recmaster role on/off
5078 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5081 uint32_t recmasterrole;
5087 if (!strcmp(argv[0], "on")) {
5089 } else if (!strcmp(argv[0], "off")) {
5095 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5097 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5105 set debug level on a node or all nodes
5107 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5113 printf("You must specify the debug level. Valid levels are:\n");
5114 for (i=0; debug_levels[i].description != NULL; i++) {
5115 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5121 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
5122 level = get_debug_by_desc(argv[0]);
5124 level = strtol(argv[0], NULL, 0);
5127 for (i=0; debug_levels[i].description != NULL; i++) {
5128 if (level == debug_levels[i].level) {
5132 if (debug_levels[i].description == NULL) {
5133 printf("Invalid debug level, must be one of\n");
5134 for (i=0; debug_levels[i].description != NULL; i++) {
5135 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5140 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5142 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5151 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5157 priority = strtol(argv[0], NULL, 0);
5161 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5163 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5165 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5172 attach to a database
5174 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5176 const char *db_name;
5177 struct ctdb_db_context *ctdb_db;
5178 bool persistent = false;
5188 if (strcmp(argv[1], "persistent") != 0) {
5194 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5195 if (ctdb_db == NULL) {
5196 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5206 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5208 struct ctdb_db_priority db_prio;
5215 db_prio.db_id = strtoul(argv[0], NULL, 0);
5216 db_prio.priority = strtoul(argv[1], NULL, 0);
5218 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5220 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5230 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5232 uint32_t db_id, priority;
5239 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5243 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5245 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5249 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5255 set the sticky records capability for a database
5257 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5259 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5267 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5271 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5273 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5274 talloc_free(tmp_ctx);
5278 talloc_free(tmp_ctx);
5283 set the readonly capability for a database
5285 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5287 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5295 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5299 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5301 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5302 talloc_free(tmp_ctx);
5306 talloc_free(tmp_ctx);
5313 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5323 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5327 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5329 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5333 printf("Sequence number:%lld\n", (long long)seqnum);
5339 run an eventscript on a node
5341 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5347 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5350 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5354 data.dptr = (unsigned char *)discard_const(argv[0]);
5355 data.dsize = strlen((char *)data.dptr) + 1;
5357 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5359 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5360 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5361 if (ret != 0 || res != 0) {
5362 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5363 talloc_free(tmp_ctx);
5366 talloc_free(tmp_ctx);
5370 #define DB_VERSION 1
5371 #define MAX_DB_NAME 64
5372 struct db_file_header {
5373 unsigned long version;
5375 unsigned long persistent;
5377 const char name[MAX_DB_NAME];
5380 struct backup_data {
5381 struct ctdb_marshall_buffer *records;
5384 bool traverse_error;
5387 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5389 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5390 struct ctdb_rec_data *rec;
5392 /* add the record */
5393 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5395 bd->traverse_error = true;
5396 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5399 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5400 if (bd->records == NULL) {
5401 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5402 bd->traverse_error = true;
5405 bd->records->count++;
5406 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5407 bd->len += rec->length;
5415 * backup a database to a file
5417 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5419 const char *db_name;
5421 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5422 struct db_file_header dbhdr;
5423 struct ctdb_db_context *ctdb_db;
5424 struct backup_data *bd;
5427 const char *reason = NULL;
5431 assert_single_node_only();
5434 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5438 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5442 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5443 db_id, tmp_ctx, &reason);
5445 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5447 talloc_free(tmp_ctx);
5451 uint32_t allow_unhealthy = 0;
5453 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5454 "AllowUnhealthyDBRead",
5457 if (allow_unhealthy != 1) {
5458 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5461 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5463 talloc_free(tmp_ctx);
5467 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5469 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5470 "tunnable AllowUnhealthyDBRead = %u\n",
5474 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5475 if (ctdb_db == NULL) {
5476 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5477 talloc_free(tmp_ctx);
5482 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5484 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5485 talloc_free(tmp_ctx);
5490 bd = talloc_zero(tmp_ctx, struct backup_data);
5492 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5493 talloc_free(tmp_ctx);
5497 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5498 if (bd->records == NULL) {
5499 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5500 talloc_free(tmp_ctx);
5504 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5505 bd->records->db_id = ctdb_db->db_id;
5506 /* traverse the database collecting all records */
5507 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5508 bd->traverse_error) {
5509 DEBUG(DEBUG_ERR,("Traverse error\n"));
5510 talloc_free(tmp_ctx);
5514 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5517 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5519 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5520 talloc_free(tmp_ctx);
5525 dbhdr.version = DB_VERSION;
5526 dbhdr.timestamp = time(NULL);
5527 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5528 dbhdr.size = bd->len;
5529 if (strlen(argv[0]) >= MAX_DB_NAME) {
5530 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5533 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5534 ret = write(fh, &dbhdr, sizeof(dbhdr));
5536 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5539 ret = write(fh, bd->records, bd->len);
5541 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5550 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5554 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5556 talloc_free(tmp_ctx);
5561 * restore a database from a file
5563 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5566 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5569 struct db_file_header dbhdr;
5570 struct ctdb_db_context *ctdb_db;
5571 struct ctdb_node_map *nodemap=NULL;
5572 struct ctdb_vnn_map *vnnmap=NULL;
5574 struct ctdb_control_wipe_database w;
5576 uint32_t generation;
5581 assert_single_node_only();
5583 if (argc < 1 || argc > 2) {
5584 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5588 fh = open(argv[0], O_RDONLY);
5590 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5591 talloc_free(tmp_ctx);
5595 read(fh, &dbhdr, sizeof(dbhdr));
5596 if (dbhdr.version != DB_VERSION) {
5597 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5599 talloc_free(tmp_ctx);
5603 dbname = discard_const(dbhdr.name);
5605 dbname = discard_const(argv[1]);
5608 outdata.dsize = dbhdr.size;
5609 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5610 if (outdata.dptr == NULL) {
5611 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5613 talloc_free(tmp_ctx);
5616 read(fh, outdata.dptr, outdata.dsize);
5619 tm = localtime(&dbhdr.timestamp);
5620 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5621 printf("Restoring database '%s' from backup @ %s\n",
5625 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5626 if (ctdb_db == NULL) {
5627 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5628 talloc_free(tmp_ctx);
5632 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5634 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5635 talloc_free(tmp_ctx);
5640 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5642 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5643 talloc_free(tmp_ctx);
5647 /* freeze all nodes */
5648 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5649 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5650 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5656 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5657 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5658 talloc_free(tmp_ctx);
5663 generation = vnnmap->generation;
5664 data.dptr = (void *)&generation;
5665 data.dsize = sizeof(generation);
5667 /* start a cluster wide transaction */
5668 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5669 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5671 TIMELIMIT(), false, data,
5674 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5679 w.db_id = ctdb_db->db_id;
5680 w.transaction_id = generation;
5682 data.dptr = (void *)&w;
5683 data.dsize = sizeof(w);
5685 /* wipe all the remote databases. */
5686 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5687 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5689 TIMELIMIT(), false, data,
5692 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5693 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5694 talloc_free(tmp_ctx);
5698 /* push the database */
5699 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5700 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5702 TIMELIMIT(), false, outdata,
5705 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5706 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5707 talloc_free(tmp_ctx);
5711 data.dptr = (void *)&ctdb_db->db_id;
5712 data.dsize = sizeof(ctdb_db->db_id);
5714 /* mark the database as healthy */
5715 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5716 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5718 TIMELIMIT(), false, data,
5721 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5722 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5723 talloc_free(tmp_ctx);
5727 data.dptr = (void *)&generation;
5728 data.dsize = sizeof(generation);
5730 /* commit all the changes */
5731 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5733 TIMELIMIT(), false, data,
5736 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5737 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5738 talloc_free(tmp_ctx);
5743 /* thaw all nodes */
5744 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5745 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5751 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5752 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5753 talloc_free(tmp_ctx);
5758 talloc_free(tmp_ctx);
5763 * dump a database backup from a file
5765 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5767 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5769 struct db_file_header dbhdr;
5773 struct ctdb_rec_data *rec = NULL;
5774 struct ctdb_marshall_buffer *m;
5775 struct ctdb_dump_db_context c;
5777 assert_single_node_only();
5780 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5784 fh = open(argv[0], O_RDONLY);
5786 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5787 talloc_free(tmp_ctx);
5791 read(fh, &dbhdr, sizeof(dbhdr));
5792 if (dbhdr.version != DB_VERSION) {
5793 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5795 talloc_free(tmp_ctx);
5799 outdata.dsize = dbhdr.size;
5800 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5801 if (outdata.dptr == NULL) {
5802 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5804 talloc_free(tmp_ctx);
5807 read(fh, outdata.dptr, outdata.dsize);
5809 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5811 tm = localtime(&dbhdr.timestamp);
5812 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5813 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5814 dbhdr.name, m->db_id, tbuf);
5818 c.printemptyrecords = (bool)options.printemptyrecords;
5819 c.printdatasize = (bool)options.printdatasize;
5820 c.printlmaster = false;
5821 c.printhash = (bool)options.printhash;
5822 c.printrecordflags = (bool)options.printrecordflags;
5824 for (i=0; i < m->count; i++) {
5828 /* we do not want the header splitted, so we pass NULL*/
5829 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5832 ctdb_dumpdb_record(ctdb, key, data, &c);
5835 printf("Dumped %d records\n", i);
5836 talloc_free(tmp_ctx);
5841 * wipe a database from a file
5843 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5846 const char *db_name;
5848 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5850 struct ctdb_db_context *ctdb_db;
5851 struct ctdb_node_map *nodemap = NULL;
5852 struct ctdb_vnn_map *vnnmap = NULL;
5854 struct ctdb_control_wipe_database w;
5856 uint32_t generation;
5859 assert_single_node_only();
5862 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5866 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5870 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5871 if (ctdb_db == NULL) {
5872 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5874 talloc_free(tmp_ctx);
5878 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5881 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5883 talloc_free(tmp_ctx);
5887 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5890 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5892 talloc_free(tmp_ctx);
5896 /* freeze all nodes */
5897 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5898 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5899 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5906 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5907 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5908 CTDB_RECOVERY_ACTIVE);
5909 talloc_free(tmp_ctx);
5914 generation = vnnmap->generation;
5915 data.dptr = (void *)&generation;
5916 data.dsize = sizeof(generation);
5918 /* start a cluster wide transaction */
5919 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5920 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5922 TIMELIMIT(), false, data,
5926 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5927 "transactions.\n"));
5931 w.db_id = ctdb_db->db_id;
5932 w.transaction_id = generation;
5934 data.dptr = (void *)&w;
5935 data.dsize = sizeof(w);
5937 /* wipe all the remote databases. */
5938 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5939 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5941 TIMELIMIT(), false, data,
5944 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5945 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5946 talloc_free(tmp_ctx);
5950 data.dptr = (void *)&ctdb_db->db_id;
5951 data.dsize = sizeof(ctdb_db->db_id);
5953 /* mark the database as healthy */
5954 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5955 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5957 TIMELIMIT(), false, data,
5960 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5961 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5962 talloc_free(tmp_ctx);
5966 data.dptr = (void *)&generation;
5967 data.dsize = sizeof(generation);
5969 /* commit all the changes */
5970 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5972 TIMELIMIT(), false, data,
5975 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5976 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5977 talloc_free(tmp_ctx);
5981 /* thaw all nodes */
5982 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5983 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5989 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5990 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5991 talloc_free(tmp_ctx);
5995 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5997 talloc_free(tmp_ctx);
6004 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6010 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6011 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6012 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6013 if (ret != 0 || res != 0) {
6014 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6015 talloc_free(tmp_ctx);
6018 write(1, data.dptr, data.dsize);
6019 talloc_free(tmp_ctx);
6024 handler for memory dumps
6026 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6027 TDB_DATA data, void *private_data)
6029 write(1, data.dptr, data.dsize);
6034 dump memory usage on the recovery daemon
6036 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6040 struct srvid_request rd;
6042 rd.pnn = ctdb_get_pnn(ctdb);
6043 rd.srvid = getpid();
6045 /* register a message port for receiveing the reply so that we
6046 can receive the reply
6048 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6051 data.dptr = (uint8_t *)&rd;
6052 data.dsize = sizeof(rd);
6054 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6056 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6060 /* this loop will terminate when we have received the reply */
6062 event_loop_once(ctdb->ev);
6069 send a message to a srvid
6071 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6073 unsigned long srvid;
6081 srvid = strtoul(argv[0], NULL, 0);
6083 data.dptr = (uint8_t *)discard_const(argv[1]);
6084 data.dsize= strlen(argv[1]);
6086 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6088 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6096 handler for msglisten
6098 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6099 TDB_DATA data, void *private_data)
6103 printf("Message received: ");
6104 for (i=0;i<data.dsize;i++) {
6105 printf("%c", data.dptr[i]);
6111 listen for messages on a messageport
6113 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6119 /* register a message port and listen for messages
6121 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6122 printf("Listening for messages on srvid:%d\n", (int)srvid);
6125 event_loop_once(ctdb->ev);
6132 list all nodes in the cluster
6133 we parse the nodes file directly
6135 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6137 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6138 struct pnn_node *pnn_nodes;
6139 struct pnn_node *pnn_node;
6141 assert_single_node_only();
6143 pnn_nodes = read_nodes_file(mem_ctx);
6144 if (pnn_nodes == NULL) {
6145 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6146 talloc_free(mem_ctx);
6150 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6151 ctdb_sock_addr addr;
6152 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
6153 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
6154 talloc_free(mem_ctx);
6157 if (options.machinereadable){
6158 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
6160 printf("%s\n", pnn_node->addr);
6163 talloc_free(mem_ctx);
6169 reload the nodes file on the local node
6171 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6175 struct ctdb_node_map *nodemap=NULL;
6177 assert_single_node_only();
6179 mypnn = ctdb_get_pnn(ctdb);
6181 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6183 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6187 /* reload the nodes file on all remote nodes */
6188 for (i=0;i<nodemap->num;i++) {
6189 if (nodemap->nodes[i].pnn == mypnn) {
6192 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6193 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6194 nodemap->nodes[i].pnn);
6196 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6200 /* reload the nodes file on the local node */
6201 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6202 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6204 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6207 /* initiate a recovery */
6208 control_recover(ctdb, argc, argv);
6214 static const struct {
6216 int (*fn)(struct ctdb_context *, int, const char **);
6218 bool without_daemon; /* can be run without daemon running ? */
6221 } ctdb_commands[] = {
6222 { "version", control_version, true, true, "show version of ctdb" },
6223 { "status", control_status, true, false, "show node status" },
6224 { "uptime", control_uptime, true, false, "show node uptime" },
6225 { "ping", control_ping, true, false, "ping all nodes" },
6226 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6227 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6228 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6229 { "listvars", control_listvars, true, false, "list tunable variables"},
6230 { "statistics", control_statistics, false, false, "show statistics" },
6231 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6232 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6233 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6234 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6235 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6236 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6237 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6238 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6239 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6240 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6241 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6242 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6243 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6244 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6245 { "lvs", control_lvs, true, false, "show lvs configuration" },
6246 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6247 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6248 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6249 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6250 { "getdebug", control_getdebug, true, false, "get debug level" },
6251 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6252 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6253 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6254 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6255 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6256 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6257 { "disable", control_disable, true, false, "disable a nodes public IP" },
6258 { "enable", control_enable, true, false, "enable a nodes public IP" },
6259 { "stop", control_stop, true, false, "stop a node" },
6260 { "continue", control_continue, true, false, "re-start a stopped node" },
6261 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6262 { "unban", control_unban, true, false, "unban a node" },
6263 { "showban", control_showban, true, false, "show ban information"},
6264 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6265 { "recover", control_recover, true, false, "force recovery" },
6266 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6267 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6268 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6269 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6270 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6271 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6272 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6273 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6274 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6276 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6278 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6279 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6280 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6281 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6282 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6283 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6284 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6285 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6286 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6287 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6288 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6289 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6290 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6291 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6292 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6293 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6294 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6295 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6296 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6297 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6298 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6299 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6300 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6301 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6302 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6303 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6304 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6305 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6306 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6307 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6308 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6309 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6310 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6311 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6312 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6313 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6314 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6315 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6316 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6317 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6318 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6319 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6320 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6321 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6322 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6323 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6324 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6325 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6326 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6332 static void usage(void)
6336 "Usage: ctdb [options] <control>\n" \
6338 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6339 " -Y generate machinereadable output\n"
6340 " -v generate verbose output\n"
6341 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6342 printf("Controls:\n");
6343 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6344 printf(" %-15s %-27s %s\n",
6345 ctdb_commands[i].name,
6346 ctdb_commands[i].args?ctdb_commands[i].args:"",
6347 ctdb_commands[i].msg);
6353 static void ctdb_alarm(int sig)
6355 printf("Maximum runtime exceeded - exiting\n");
6362 int main(int argc, const char *argv[])
6364 struct ctdb_context *ctdb;
6365 char *nodestring = NULL;
6366 struct poptOption popt_options[] = {
6369 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6370 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6371 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6372 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6373 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6374 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6375 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6376 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6377 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6378 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6382 const char **extra_argv;
6386 struct event_context *ev;
6387 const char *control;
6391 /* set some defaults */
6392 options.maxruntime = 0;
6393 options.timelimit = 10;
6394 options.pnn = CTDB_CURRENT_NODE;
6396 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6398 while ((opt = poptGetNextOpt(pc)) != -1) {
6401 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6402 poptBadOption(pc, 0), poptStrerror(opt)));
6407 /* setup the remaining options for the main program to use */
6408 extra_argv = poptGetArgs(pc);
6411 while (extra_argv[extra_argc]) extra_argc++;
6414 if (extra_argc < 1) {
6418 if (options.maxruntime == 0) {
6419 const char *ctdb_timeout;
6420 ctdb_timeout = getenv("CTDB_TIMEOUT");
6421 if (ctdb_timeout != NULL) {
6422 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6424 /* default timeout is 120 seconds */
6425 options.maxruntime = 120;
6429 signal(SIGALRM, ctdb_alarm);
6430 alarm(options.maxruntime);
6432 control = extra_argv[0];
6434 /* Default value for CTDB_BASE - don't override */
6435 setenv("CTDB_BASE", ETCDIR "/ctdb", 0);
6437 ev = event_context_init(NULL);
6439 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6443 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6444 if (strcmp(control, ctdb_commands[i].name) == 0) {
6449 if (i == ARRAY_SIZE(ctdb_commands)) {
6450 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6454 if (ctdb_commands[i].without_daemon == true) {
6455 if (nodestring != NULL) {
6456 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6459 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6462 /* initialise ctdb */
6463 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6466 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6470 /* setup the node number(s) to contact */
6471 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6472 &options.nodes, &options.pnn)) {
6476 if (options.pnn == CTDB_CURRENT_NODE) {
6477 options.pnn = options.nodes[0];
6480 if (ctdb_commands[i].auto_all &&
6481 ((options.pnn == CTDB_BROADCAST_ALL) ||
6482 (options.pnn == CTDB_MULTICAST))) {
6486 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6487 options.pnn = options.nodes[j];
6488 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6491 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6496 (void)poptFreeContext(pc);