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) {
226 i = (uint32_t)strtoul(tok, NULL, 0);
227 if (i >= nodemap->num) {
228 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
229 talloc_free(tmp_ctx);
232 if ((nodemap->nodes[i].flags &
233 (NODE_FLAGS_DISCONNECTED |
234 NODE_FLAGS_DELETED)) && !dd_ok) {
235 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
236 talloc_free(tmp_ctx);
239 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
240 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
241 talloc_free(tmp_ctx);
245 *nodes = talloc_realloc(mem_ctx, *nodes,
247 if (*nodes == NULL) {
254 tok = strtok(NULL, ",");
259 *pnn_mode = (*nodes)[0];
261 *pnn_mode = CTDB_MULTICAST;
265 /* default - no nodes specified */
266 *nodes = talloc_array(mem_ctx, uint32_t, 1);
267 if (*nodes == NULL) {
270 *pnn_mode = CTDB_CURRENT_NODE;
272 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
277 talloc_free(tmp_ctx);
281 talloc_free(tmp_ctx);
286 check if a database exists
288 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg, uint32_t *dbid, uint8_t *flags)
291 struct ctdb_dbid_map *dbmap=NULL;
292 bool dbid_given = false, found = false;
294 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
296 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
298 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
302 if (strncmp(dbarg, "0x", 2) == 0) {
303 id = strtoul(dbarg, NULL, 0);
307 for(i=0; i<dbmap->num; i++) {
309 if (id == dbmap->dbs[i].dbid) {
315 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
317 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
321 if (strcmp(name, dbarg) == 0) {
322 id = dbmap->dbs[i].dbid;
330 if (dbid) *dbid = id;
331 if (flags) *flags = dbmap->dbs[i].flags;
333 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
337 talloc_free(tmp_ctx);
342 see if a process exists
344 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
352 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
353 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
357 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
359 printf("%u:%u exists\n", pnn, pid);
361 printf("%u:%u does not exist\n", pnn, pid);
367 display statistics structure
369 static void show_statistics(struct ctdb_statistics *s, int show_header)
371 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
373 const char *prefix=NULL;
375 int tmp, days, hours, minutes, seconds;
380 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
381 STATISTICS_FIELD(num_clients),
382 STATISTICS_FIELD(frozen),
383 STATISTICS_FIELD(recovering),
384 STATISTICS_FIELD(num_recoveries),
385 STATISTICS_FIELD(client_packets_sent),
386 STATISTICS_FIELD(client_packets_recv),
387 STATISTICS_FIELD(node_packets_sent),
388 STATISTICS_FIELD(node_packets_recv),
389 STATISTICS_FIELD(keepalive_packets_sent),
390 STATISTICS_FIELD(keepalive_packets_recv),
391 STATISTICS_FIELD(node.req_call),
392 STATISTICS_FIELD(node.reply_call),
393 STATISTICS_FIELD(node.req_dmaster),
394 STATISTICS_FIELD(node.reply_dmaster),
395 STATISTICS_FIELD(node.reply_error),
396 STATISTICS_FIELD(node.req_message),
397 STATISTICS_FIELD(node.req_control),
398 STATISTICS_FIELD(node.reply_control),
399 STATISTICS_FIELD(client.req_call),
400 STATISTICS_FIELD(client.req_message),
401 STATISTICS_FIELD(client.req_control),
402 STATISTICS_FIELD(timeouts.call),
403 STATISTICS_FIELD(timeouts.control),
404 STATISTICS_FIELD(timeouts.traverse),
405 STATISTICS_FIELD(locks.num_calls),
406 STATISTICS_FIELD(locks.num_current),
407 STATISTICS_FIELD(locks.num_pending),
408 STATISTICS_FIELD(locks.num_failed),
409 STATISTICS_FIELD(total_calls),
410 STATISTICS_FIELD(pending_calls),
411 STATISTICS_FIELD(childwrite_calls),
412 STATISTICS_FIELD(pending_childwrite_calls),
413 STATISTICS_FIELD(memory_used),
414 STATISTICS_FIELD(max_hop_count),
415 STATISTICS_FIELD(total_ro_delegations),
416 STATISTICS_FIELD(total_ro_revokes),
419 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
428 if (options.machinereadable){
430 printf("CTDB version:");
431 printf("Current time of statistics:");
432 printf("Statistics collected since:");
433 for (i=0;i<ARRAY_SIZE(fields);i++) {
434 printf("%s:", fields[i].name);
436 printf("num_reclock_ctdbd_latency:");
437 printf("min_reclock_ctdbd_latency:");
438 printf("avg_reclock_ctdbd_latency:");
439 printf("max_reclock_ctdbd_latency:");
441 printf("num_reclock_recd_latency:");
442 printf("min_reclock_recd_latency:");
443 printf("avg_reclock_recd_latency:");
444 printf("max_reclock_recd_latency:");
446 printf("num_call_latency:");
447 printf("min_call_latency:");
448 printf("avg_call_latency:");
449 printf("max_call_latency:");
451 printf("num_lockwait_latency:");
452 printf("min_lockwait_latency:");
453 printf("avg_lockwait_latency:");
454 printf("max_lockwait_latency:");
456 printf("num_childwrite_latency:");
457 printf("min_childwrite_latency:");
458 printf("avg_childwrite_latency:");
459 printf("max_childwrite_latency:");
462 printf("%d:", CTDB_VERSION);
463 printf("%d:", (int)s->statistics_current_time.tv_sec);
464 printf("%d:", (int)s->statistics_start_time.tv_sec);
465 for (i=0;i<ARRAY_SIZE(fields);i++) {
466 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
468 printf("%d:", s->reclock.ctdbd.num);
469 printf("%.6f:", s->reclock.ctdbd.min);
470 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
471 printf("%.6f:", s->reclock.ctdbd.max);
473 printf("%d:", s->reclock.recd.num);
474 printf("%.6f:", s->reclock.recd.min);
475 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
476 printf("%.6f:", s->reclock.recd.max);
478 printf("%d:", s->call_latency.num);
479 printf("%.6f:", s->call_latency.min);
480 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
481 printf("%.6f:", s->call_latency.max);
483 printf("%d:", s->childwrite_latency.num);
484 printf("%.6f:", s->childwrite_latency.min);
485 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
486 printf("%.6f:", s->childwrite_latency.max);
489 printf("CTDB version %u\n", CTDB_VERSION);
490 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
491 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
493 for (i=0;i<ARRAY_SIZE(fields);i++) {
494 if (strchr(fields[i].name, '.')) {
495 preflen = strcspn(fields[i].name, ".")+1;
496 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
497 prefix = fields[i].name;
498 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
503 printf(" %*s%-22s%*s%10u\n",
505 fields[i].name+preflen,
507 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
509 printf(" hop_count_buckets:");
510 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
511 printf(" %d", s->hop_count_bucket[i]);
514 printf(" lock_buckets:");
515 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
516 printf(" %d", s->locks.buckets[i]);
519 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);
521 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);
523 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);
525 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);
526 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);
529 talloc_free(tmp_ctx);
533 display remote ctdb statistics combined from all nodes
535 static int control_statistics_all(struct ctdb_context *ctdb)
538 struct ctdb_statistics statistics;
542 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
543 CTDB_NO_MEMORY(ctdb, nodes);
545 ZERO_STRUCT(statistics);
547 for (i=0;i<num_nodes;i++) {
548 struct ctdb_statistics s1;
550 uint32_t *v1 = (uint32_t *)&s1;
551 uint32_t *v2 = (uint32_t *)&statistics;
553 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
554 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
556 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
559 for (j=0;j<num_ints;j++) {
562 statistics.max_hop_count =
563 MAX(statistics.max_hop_count, s1.max_hop_count);
564 statistics.call_latency.max =
565 MAX(statistics.call_latency.max, s1.call_latency.max);
568 printf("Gathered statistics for %u nodes\n", num_nodes);
569 show_statistics(&statistics, 1);
574 display remote ctdb statistics
576 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
579 struct ctdb_statistics statistics;
581 if (options.pnn == CTDB_BROADCAST_ALL) {
582 return control_statistics_all(ctdb);
585 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
587 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
590 show_statistics(&statistics, 1);
596 reset remote ctdb statistics
598 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
602 ret = ctdb_statistics_reset(ctdb, options.pnn);
604 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
612 display remote ctdb rolling statistics
614 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
617 struct ctdb_statistics_wire *stats;
618 int i, num_records = -1;
620 assert_single_node_only();
623 num_records = atoi(argv[0]) - 1;
626 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
628 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
631 for (i=0;i<stats->num;i++) {
632 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
635 show_statistics(&stats->stats[i], i==0);
636 if (i == num_records) {
645 display remote ctdb db statistics
647 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
649 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
650 struct ctdb_db_statistics *dbstat;
660 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
664 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
666 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
667 talloc_free(tmp_ctx);
671 printf("DB Statistics: %s\n", argv[0]);
672 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
673 dbstat->db_ro_delegations);
674 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
675 dbstat->db_ro_delegations);
676 printf(" %s\n", "locks");
677 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
678 dbstat->locks.num_calls);
679 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
680 dbstat->locks.num_failed);
681 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
682 dbstat->locks.num_current);
683 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
684 dbstat->locks.num_pending);
685 printf(" %s", "hop_count_buckets:");
686 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
687 printf(" %d", dbstat->hop_count_bucket[i]);
690 printf(" %s", "lock_buckets:");
691 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
692 printf(" %d", dbstat->locks.buckets[i]);
695 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
696 "locks_latency MIN/AVG/MAX",
697 dbstat->locks.latency.min,
698 (dbstat->locks.latency.num ?
699 dbstat->locks.latency.total /dbstat->locks.latency.num :
701 dbstat->locks.latency.max,
702 dbstat->locks.latency.num);
704 for (i=0; i<dbstat->num_hot_keys; i++) {
705 if (dbstat->hot_keys[i].count > 0) {
709 dbstat->num_hot_keys = num_hot_keys;
711 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
712 for (i = 0; i < dbstat->num_hot_keys; i++) {
714 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
715 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
716 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
721 talloc_free(tmp_ctx);
726 display uptime of remote node
728 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
731 struct ctdb_uptime *uptime = NULL;
732 int tmp, days, hours, minutes, seconds;
734 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
736 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
740 if (options.machinereadable){
741 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
742 printf(":%u:%u:%u:%lf\n",
743 (unsigned int)uptime->current_time.tv_sec,
744 (unsigned int)uptime->ctdbd_start_time.tv_sec,
745 (unsigned int)uptime->last_recovery_finished.tv_sec,
746 timeval_delta(&uptime->last_recovery_finished,
747 &uptime->last_recovery_started)
752 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
754 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
762 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
764 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
772 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
774 printf("Duration of last recovery/failover: %lf seconds\n",
775 timeval_delta(&uptime->last_recovery_finished,
776 &uptime->last_recovery_started));
782 show the PNN of the current node
784 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
788 mypnn = getpnn(ctdb);
790 printf("PNN:%d\n", mypnn);
796 struct pnn_node *next;
801 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
803 const char *nodes_list;
807 struct pnn_node *pnn_nodes = NULL;
808 struct pnn_node *pnn_node;
809 struct pnn_node *tmp_node;
811 /* read the nodes file */
812 nodes_list = getenv("CTDB_NODES");
813 if (nodes_list == NULL) {
814 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
815 getenv("CTDB_BASE"));
816 if (nodes_list == NULL) {
817 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
821 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
825 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
828 for (i=0, pnn=0; i<nlines; i++) {
832 /* strip leading spaces */
833 while((*node == ' ') || (*node == '\t')) {
840 if (strcmp(node, "") == 0) {
843 pnn_node = talloc(mem_ctx, struct pnn_node);
844 pnn_node->pnn = pnn++;
845 pnn_node->addr = talloc_strdup(pnn_node, node);
846 pnn_node->next = pnn_nodes;
847 pnn_nodes = pnn_node;
850 /* swap them around so we return them in incrementing order */
851 pnn_node = pnn_nodes;
855 pnn_node = pnn_node->next;
857 tmp_node->next = pnn_nodes;
858 pnn_nodes = tmp_node;
865 show the PNN of the current node
866 discover the pnn by loading the nodes file and try to bind to all
867 addresses one at a time until the ip address is found.
869 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
871 TALLOC_CTX *mem_ctx = talloc_new(NULL);
872 struct pnn_node *pnn_nodes;
873 struct pnn_node *pnn_node;
875 assert_single_node_only();
877 pnn_nodes = read_nodes_file(mem_ctx);
878 if (pnn_nodes == NULL) {
879 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
880 talloc_free(mem_ctx);
884 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
887 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
888 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
889 talloc_free(mem_ctx);
893 if (ctdb_sys_have_ip(&addr)) {
894 printf("PNN:%d\n", pnn_node->pnn);
895 talloc_free(mem_ctx);
900 printf("Failed to detect which PNN this node is\n");
901 talloc_free(mem_ctx);
905 /* Helpers for ctdb status
907 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
909 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
913 if (node->flags == 0) {
914 struct ctdb_control_get_ifaces *ifaces;
916 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
917 tmp_ctx, &ifaces) == 0) {
918 for (j=0; j < ifaces->num; j++) {
919 if (ifaces->ifaces[j].link_state != 0) {
927 talloc_free(tmp_ctx);
932 static void control_status_header_machine(void)
934 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
935 ":Inactive:PartiallyOnline:ThisNode:\n");
938 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
939 struct ctdb_node_and_flags *node)
941 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
942 ctdb_addr_to_str(&node->addr),
943 !!(node->flags&NODE_FLAGS_DISCONNECTED),
944 !!(node->flags&NODE_FLAGS_BANNED),
945 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
946 !!(node->flags&NODE_FLAGS_UNHEALTHY),
947 !!(node->flags&NODE_FLAGS_STOPPED),
948 !!(node->flags&NODE_FLAGS_INACTIVE),
949 is_partially_online(ctdb, node) ? 1 : 0,
950 (node->pnn == mypnn)?'Y':'N');
955 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
956 struct ctdb_node_and_flags *node)
958 printf("pnn:%d %-16s %s%s\n", node->pnn,
959 ctdb_addr_to_str(&node->addr),
960 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
961 node->pnn == mypnn?" (THIS NODE)":"");
967 display remote ctdb status
969 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
971 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
973 struct ctdb_vnn_map *vnnmap=NULL;
974 struct ctdb_node_map *nodemap=NULL;
975 uint32_t recmode, recmaster, mypnn;
976 int num_deleted_nodes = 0;
979 mypnn = getpnn(ctdb);
981 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
983 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
984 talloc_free(tmp_ctx);
988 if (options.machinereadable) {
989 control_status_header_machine();
990 for (i=0;i<nodemap->num;i++) {
991 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
994 (void) control_status_1_machine(ctdb, mypnn,
997 talloc_free(tmp_ctx);
1001 for (i=0; i<nodemap->num; i++) {
1002 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1003 num_deleted_nodes++;
1006 if (num_deleted_nodes == 0) {
1007 printf("Number of nodes:%d\n", nodemap->num);
1009 printf("Number of nodes:%d (including %d deleted nodes)\n",
1010 nodemap->num, num_deleted_nodes);
1012 for(i=0;i<nodemap->num;i++){
1013 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1016 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1019 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1021 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1022 talloc_free(tmp_ctx);
1025 if (vnnmap->generation == INVALID_GENERATION) {
1026 printf("Generation:INVALID\n");
1028 printf("Generation:%d\n",vnnmap->generation);
1030 printf("Size:%d\n",vnnmap->size);
1031 for(i=0;i<vnnmap->size;i++){
1032 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1035 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1037 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1038 talloc_free(tmp_ctx);
1041 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1043 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1045 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1046 talloc_free(tmp_ctx);
1049 printf("Recovery master:%d\n",recmaster);
1051 talloc_free(tmp_ctx);
1055 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1057 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1059 struct ctdb_node_map *nodemap=NULL;
1061 uint32_t pnn_mode, mypnn;
1067 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1068 options.pnn, true, &nodes, &pnn_mode)) {
1072 if (options.machinereadable) {
1073 control_status_header_machine();
1074 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1075 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1078 mypnn = getpnn(ctdb);
1080 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1082 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1083 talloc_free(tmp_ctx);
1089 for (i = 0; i < talloc_array_length(nodes); i++) {
1090 if (options.machinereadable) {
1091 ret |= control_status_1_machine(ctdb, mypnn,
1092 &nodemap->nodes[nodes[i]]);
1094 ret |= control_status_1_human(ctdb, mypnn,
1095 &nodemap->nodes[nodes[i]]);
1099 talloc_free(tmp_ctx);
1104 struct natgw_node *next;
1108 static int find_natgw(struct ctdb_context *ctdb,
1109 struct ctdb_node_map *nodemap, uint32_t flags,
1110 uint32_t *pnn, const char **ip)
1113 uint32_t capabilities;
1116 for (i=0;i<nodemap->num;i++) {
1117 if (!(nodemap->nodes[i].flags & flags)) {
1118 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1119 nodemap->nodes[i].pnn,
1122 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1123 nodemap->nodes[i].pnn));
1126 if (!(capabilities&CTDB_CAP_NATGW)) {
1129 *pnn = nodemap->nodes[i].pnn;
1130 *ip = ctdb_addr_to_str(&nodemap->nodes[i].addr);
1135 return 2; /* matches ENOENT */
1139 display the list of nodes belonging to this natgw configuration
1141 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1143 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1145 const char *natgw_list;
1148 struct natgw_node *natgw_nodes = NULL;
1149 struct natgw_node *natgw_node;
1150 struct ctdb_node_map *nodemap=NULL;
1151 uint32_t mypnn, pnn;
1154 /* When we have some nodes that could be the NATGW, make a
1155 * series of attempts to find the first node that doesn't have
1156 * certain status flags set.
1158 uint32_t exclude_flags[] = {
1159 /* Look for a nice healthy node */
1160 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1161 /* If not found, an UNHEALTHY/BANNED node will do */
1162 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1163 /* If not found, a STOPPED node will do */
1164 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1168 /* read the natgw nodes file into a linked list */
1169 natgw_list = getenv("CTDB_NATGW_NODES");
1170 if (natgw_list == NULL) {
1171 natgw_list = talloc_asprintf(tmp_ctx, "%s/natgw_nodes",
1172 getenv("CTDB_BASE"));
1173 if (natgw_list == NULL) {
1174 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1178 lines = file_lines_load(natgw_list, &nlines, ctdb);
1179 if (lines == NULL) {
1180 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
1181 talloc_free(tmp_ctx);
1184 for (i=0;i<nlines;i++) {
1188 /* strip leading spaces */
1189 while((*node == ' ') || (*node == '\t')) {
1195 if (strcmp(node, "") == 0) {
1198 natgw_node = talloc(ctdb, struct natgw_node);
1199 natgw_node->addr = talloc_strdup(natgw_node, node);
1200 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1201 natgw_node->next = natgw_nodes;
1202 natgw_nodes = natgw_node;
1205 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1207 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1208 talloc_free(tmp_ctx);
1212 /* Trim the nodemap so it only includes connected nodes in the
1213 * current natgw group.
1216 while(i<nodemap->num) {
1217 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
1218 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1223 /* this node was not in the natgw so we just remove it from
1226 if ((natgw_node == NULL)
1227 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1230 for (j=i+1; j<nodemap->num; j++) {
1231 nodemap->nodes[j-1] = nodemap->nodes[j];
1240 ret = 2; /* matches ENOENT */
1243 for (i = 0; exclude_flags[i] != 0; i++) {
1244 ret = find_natgw(ctdb, nodemap,
1255 if (options.machinereadable) {
1256 printf(":Node:IP:\n");
1257 printf(":%d:%s:\n", pnn, ip);
1259 printf("%d %s\n", pnn, ip);
1262 /* print the pruned list of nodes belonging to this natgw list */
1263 mypnn = getpnn(ctdb);
1264 if (options.machinereadable) {
1265 control_status_header_machine();
1267 printf("Number of nodes:%d\n", nodemap->num);
1269 for(i=0;i<nodemap->num;i++){
1270 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1273 if (options.machinereadable) {
1274 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1276 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1281 talloc_free(tmp_ctx);
1286 display the status of the scripts for monitoring (or other events)
1288 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1289 enum ctdb_eventscript_call type)
1291 struct ctdb_scripts_wire *script_status;
1294 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1296 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1300 if (script_status == NULL) {
1301 if (!options.machinereadable) {
1302 printf("%s cycle never run\n",
1303 ctdb_eventscript_call_names[type]);
1308 if (!options.machinereadable) {
1309 printf("%d scripts were executed last %s cycle\n",
1310 script_status->num_scripts,
1311 ctdb_eventscript_call_names[type]);
1313 for (i=0; i<script_status->num_scripts; i++) {
1314 const char *status = NULL;
1316 switch (script_status->scripts[i].status) {
1318 status = "TIMEDOUT";
1321 status = "DISABLED";
1327 if (script_status->scripts[i].status > 0)
1331 if (options.machinereadable) {
1332 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1333 ctdb_eventscript_call_names[type],
1334 script_status->scripts[i].name,
1335 script_status->scripts[i].status,
1337 (long)script_status->scripts[i].start.tv_sec,
1338 (long)script_status->scripts[i].start.tv_usec,
1339 (long)script_status->scripts[i].finished.tv_sec,
1340 (long)script_status->scripts[i].finished.tv_usec,
1341 script_status->scripts[i].output);
1345 printf("%-20s Status:%s ",
1346 script_status->scripts[i].name, status);
1348 /* Some other error, eg from stat. */
1349 printf("%-20s Status:CANNOT RUN (%s)",
1350 script_status->scripts[i].name,
1351 strerror(-script_status->scripts[i].status));
1353 if (script_status->scripts[i].status >= 0) {
1354 printf("Duration:%.3lf ",
1355 timeval_delta(&script_status->scripts[i].finished,
1356 &script_status->scripts[i].start));
1358 if (script_status->scripts[i].status != -ENOEXEC) {
1360 ctime(&script_status->scripts[i].start.tv_sec));
1361 if (script_status->scripts[i].status != 0) {
1362 printf(" OUTPUT:%s\n",
1363 script_status->scripts[i].output);
1373 static int control_scriptstatus(struct ctdb_context *ctdb,
1374 int argc, const char **argv)
1377 enum ctdb_eventscript_call type, min, max;
1381 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1386 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1390 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1391 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1397 if (type == CTDB_EVENT_MAX) {
1398 if (strcmp(arg, "all") == 0) {
1400 max = CTDB_EVENT_MAX;
1402 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1407 if (options.machinereadable) {
1408 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1411 for (type = min; type < max; type++) {
1412 ret = control_one_scriptstatus(ctdb, type);
1422 enable an eventscript
1424 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1432 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1434 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1442 disable an eventscript
1444 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1452 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1454 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1462 display the pnn of the recovery master
1464 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1469 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1471 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1474 printf("%d\n",recmaster);
1480 add a tickle to a public address
1482 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1484 struct ctdb_tcp_connection t;
1488 assert_single_node_only();
1494 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1495 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1498 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1499 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1503 data.dptr = (uint8_t *)&t;
1504 data.dsize = sizeof(t);
1506 /* tell all nodes about this tcp connection */
1507 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1508 0, data, ctdb, NULL, NULL, NULL, NULL);
1510 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1519 delete a tickle from a node
1521 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1523 struct ctdb_tcp_connection t;
1527 assert_single_node_only();
1533 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1534 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1537 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1538 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1542 data.dptr = (uint8_t *)&t;
1543 data.dsize = sizeof(t);
1545 /* tell all nodes about this tcp connection */
1546 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1547 0, data, ctdb, NULL, NULL, NULL, NULL);
1549 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1558 get a list of all tickles for this pnn
1560 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1562 struct ctdb_control_tcp_tickle_list *list;
1563 ctdb_sock_addr addr;
1567 assert_single_node_only();
1574 port = atoi(argv[1]);
1577 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1578 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1582 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1584 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1588 if (options.machinereadable){
1589 printf(":source ip:port:destination ip:port:\n");
1590 for (i=0;i<list->tickles.num;i++) {
1591 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1594 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1595 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1598 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1599 printf("Num tickles:%u\n", list->tickles.num);
1600 for (i=0;i<list->tickles.num;i++) {
1601 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1604 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1605 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));
1615 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1617 struct ctdb_all_public_ips *ips;
1618 struct ctdb_public_ip ip;
1621 uint32_t disable_time;
1623 struct ctdb_node_map *nodemap=NULL;
1624 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1627 data.dptr = (uint8_t*)&disable_time;
1628 data.dsize = sizeof(disable_time);
1629 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1631 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1637 /* read the public ip list from the node */
1638 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1640 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1641 talloc_free(tmp_ctx);
1645 for (i=0;i<ips->num;i++) {
1646 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1651 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1652 pnn, ctdb_addr_to_str(addr)));
1653 talloc_free(tmp_ctx);
1660 data.dptr = (uint8_t *)&ip;
1661 data.dsize = sizeof(ip);
1663 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1665 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1666 talloc_free(tmp_ctx);
1670 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1671 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1678 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1679 talloc_free(tmp_ctx);
1683 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1685 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1686 talloc_free(tmp_ctx);
1690 /* update the recovery daemon so it now knows to expect the new
1691 node assignment for this ip.
1693 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1695 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1699 talloc_free(tmp_ctx);
1705 * scans all other nodes and returns a pnn for another node that can host this
1709 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1711 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1712 struct ctdb_all_public_ips *ips;
1713 struct ctdb_node_map *nodemap=NULL;
1716 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1718 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1719 talloc_free(tmp_ctx);
1723 for(i=0;i<nodemap->num;i++){
1724 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1727 if (nodemap->nodes[i].pnn == options.pnn) {
1731 /* read the public ip list from this node */
1732 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1734 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1738 for (j=0;j<ips->num;j++) {
1739 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1740 talloc_free(tmp_ctx);
1741 return nodemap->nodes[i].pnn;
1747 talloc_free(tmp_ctx);
1751 /* If pnn is -1 then try to find a node to move IP to... */
1752 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1754 bool pnn_specified = (pnn == -1 ? false : true);
1757 while (retries < 5) {
1758 if (!pnn_specified) {
1759 pnn = find_other_host_for_public_ip(ctdb, addr);
1764 ("Trying to move public IP to node %u\n", pnn));
1767 if (move_ip(ctdb, addr, pnn) == 0) {
1780 move/failover an ip address to a specific node
1782 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1785 ctdb_sock_addr addr;
1787 assert_single_node_only();
1794 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1795 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1800 if (sscanf(argv[1], "%u", &pnn) != 1) {
1801 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1805 if (!try_moveip(ctdb, &addr, pnn)) {
1806 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1813 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1817 data.dptr = (uint8_t *)&pnn;
1818 data.dsize = sizeof(uint32_t);
1819 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1821 ("Failed to send message to force node %u to be a rebalancing target\n",
1831 rebalance a node by setting it to allow failback and triggering a
1834 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1836 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1841 assert_single_node_only();
1847 /* Determine the nodes where IPs need to be reloaded */
1848 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1849 options.pnn, true, &nodes, &pnn_mode)) {
1854 for (i = 0; i < talloc_array_length(nodes); i++) {
1855 if (!rebalance_node(ctdb, nodes[i])) {
1861 talloc_free(tmp_ctx);
1865 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1867 struct ctdb_public_ip ip;
1870 uint32_t disable_time;
1872 struct ctdb_node_map *nodemap=NULL;
1873 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1876 data.dptr = (uint8_t*)&disable_time;
1877 data.dsize = sizeof(disable_time);
1878 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1880 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1887 data.dptr = (uint8_t *)&ip;
1888 data.dsize = sizeof(ip);
1890 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1892 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1893 talloc_free(tmp_ctx);
1897 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1898 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1905 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1906 talloc_free(tmp_ctx);
1910 talloc_free(tmp_ctx);
1915 release an ip form all nodes and have it re-assigned by recd
1917 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1919 ctdb_sock_addr addr;
1921 assert_single_node_only();
1928 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1929 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1933 if (rebalance_ip(ctdb, &addr) != 0) {
1934 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
1941 static int getips_store_callback(void *param, void *data)
1943 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1944 struct ctdb_all_public_ips *ips = param;
1948 ips->ips[i].pnn = node_ip->pnn;
1949 ips->ips[i].addr = node_ip->addr;
1953 static int getips_count_callback(void *param, void *data)
1955 uint32_t *count = param;
1962 static uint32_t *ip_key(ctdb_sock_addr *ip)
1964 static uint32_t key[IP_KEYLEN];
1966 bzero(key, sizeof(key));
1968 switch (ip->sa.sa_family) {
1970 key[0] = ip->ip.sin_addr.s_addr;
1973 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1981 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1988 static void *add_ip_callback(void *parm, void *data)
1994 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1996 struct ctdb_all_public_ips *tmp_ips;
1997 struct ctdb_node_map *nodemap=NULL;
1998 trbt_tree_t *ip_tree;
2002 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2004 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2008 ip_tree = trbt_create(tmp_ctx, 0);
2010 for(i=0;i<nodemap->num;i++){
2011 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2014 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2018 /* read the public ip list from this node */
2019 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2021 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2025 for (j=0; j<tmp_ips->num;j++) {
2026 struct ctdb_public_ip *node_ip;
2028 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2029 node_ip->pnn = tmp_ips->ips[j].pnn;
2030 node_ip->addr = tmp_ips->ips[j].addr;
2032 trbt_insertarray32_callback(ip_tree,
2033 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2037 talloc_free(tmp_ips);
2042 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2044 len = offsetof(struct ctdb_all_public_ips, ips) +
2045 count*sizeof(struct ctdb_public_ip);
2046 tmp_ips = talloc_zero_size(tmp_ctx, len);
2047 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2055 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2057 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2059 event_add_timed(ctdb->ev, ctdb,
2060 timeval_current_ofs(1, 0),
2061 ctdb_every_second, ctdb);
2064 struct srvid_reply_handler_data {
2068 const char *srvid_str;
2071 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2076 struct srvid_reply_handler_data *d =
2077 (struct srvid_reply_handler_data *)private_data;
2081 if (data.dsize != sizeof(ret)) {
2082 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2086 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2087 ret = *(int32_t *)data.dptr;
2090 ("%s failed with result %d\n", d->srvid_str, ret));
2094 if (!d->wait_for_all) {
2099 /* Wait for all replies */
2101 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2102 if (d->nodes[i] == ret) {
2104 ("%s reply received from node %u\n",
2105 d->srvid_str, ret));
2108 if (d->nodes[i] != -1) {
2109 /* Found a node that hasn't yet replied */
2115 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2116 * or replies from all connected nodes. arg is the data argument to
2117 * pass in the srvid_request structure - pass 0 if this isn't needed.
2119 static int srvid_broadcast(struct ctdb_context *ctdb,
2120 uint64_t srvid, uint32_t arg,
2121 const char *srvid_str, bool wait_for_all)
2125 struct srvid_request request;
2126 struct srvid_reply_handler_data reply_data;
2129 ZERO_STRUCT(request);
2131 /* Time ticks to enable timeouts to be processed */
2132 event_add_timed(ctdb->ev, ctdb,
2133 timeval_current_ofs(1, 0),
2134 ctdb_every_second, ctdb);
2136 request.pnn = ctdb_get_pnn(ctdb);
2137 request.srvid = getpid();
2140 /* Register message port for reply from recovery master */
2141 ctdb_client_set_message_handler(ctdb, request.srvid,
2142 srvid_broadcast_reply_handler,
2145 data.dptr = (uint8_t *)&request;
2146 data.dsize = sizeof(request);
2148 reply_data.wait_for_all = wait_for_all;
2149 reply_data.nodes = NULL;
2150 reply_data.srvid_str = srvid_str;
2153 reply_data.done = false;
2156 struct ctdb_node_map *nodemap;
2158 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2159 CTDB_CURRENT_NODE, ctdb, &nodemap);
2162 ("Unable to get nodemap from current node, try again\n"));
2167 if (reply_data.nodes != NULL) {
2168 talloc_free(reply_data.nodes);
2170 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2173 talloc_free(nodemap);
2176 /* Send to all connected nodes. Only recmaster replies */
2177 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2180 /* This can only happen if the socket is closed and
2181 * there's no way to recover from that, so don't try
2185 ("Failed to send %s request to connected nodes\n",
2190 tv = timeval_current();
2191 /* This loop terminates the reply is received */
2192 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2193 event_loop_once(ctdb->ev);
2196 if (!reply_data.done) {
2198 ("Still waiting for confirmation of %s\n", srvid_str));
2203 ctdb_client_remove_message_handler(ctdb, request.srvid, &reply_data);
2205 talloc_free(reply_data.nodes);
2210 static int ipreallocate(struct ctdb_context *ctdb)
2212 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, 0,
2213 "IP reallocation", false);
2217 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2219 return ipreallocate(ctdb);
2223 add a public ip address to a node
2225 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2228 int len, retries = 0;
2230 ctdb_sock_addr addr;
2231 struct ctdb_control_ip_iface *pub;
2232 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2233 struct ctdb_all_public_ips *ips;
2237 talloc_free(tmp_ctx);
2241 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2242 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2243 talloc_free(tmp_ctx);
2247 /* read the public ip list from the node */
2248 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2250 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2251 talloc_free(tmp_ctx);
2254 for (i=0;i<ips->num;i++) {
2255 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2256 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2263 /* Dont timeout. This command waits for an ip reallocation
2264 which sometimes can take wuite a while if there has
2265 been a recent recovery
2269 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2270 pub = talloc_size(tmp_ctx, len);
2271 CTDB_NO_MEMORY(ctdb, pub);
2275 pub->len = strlen(argv[1])+1;
2276 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2279 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2281 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2285 } while (retries < 5 && ret != 0);
2287 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2288 talloc_free(tmp_ctx);
2292 if (rebalance_node(ctdb, options.pnn) != 0) {
2293 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2297 talloc_free(tmp_ctx);
2302 add a public ip address to a node
2304 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2306 ctdb_sock_addr addr;
2312 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2313 printf("Badly formed ip : %s\n", argv[0]);
2317 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2322 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2324 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2326 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2327 struct ctdb_node_map *nodemap=NULL;
2328 struct ctdb_all_public_ips *ips;
2331 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2333 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2337 /* remove it from the nodes that are not hosting the ip currently */
2338 for(i=0;i<nodemap->num;i++){
2339 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2342 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2344 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2348 for (j=0;j<ips->num;j++) {
2349 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2357 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2361 options.pnn = nodemap->nodes[i].pnn;
2362 control_delip(ctdb, argc, argv);
2366 /* remove it from every node (also the one hosting it) */
2367 for(i=0;i<nodemap->num;i++){
2368 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2371 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2373 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2377 for (j=0;j<ips->num;j++) {
2378 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2386 options.pnn = nodemap->nodes[i].pnn;
2387 control_delip(ctdb, argc, argv);
2390 talloc_free(tmp_ctx);
2395 delete a public ip address from a node
2397 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2400 ctdb_sock_addr addr;
2401 struct ctdb_control_ip_iface pub;
2402 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2403 struct ctdb_all_public_ips *ips;
2406 talloc_free(tmp_ctx);
2410 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2411 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2415 if (options.pnn == CTDB_BROADCAST_ALL) {
2416 return control_delip_all(ctdb, argc, argv, &addr);
2423 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2425 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2426 talloc_free(tmp_ctx);
2430 for (i=0;i<ips->num;i++) {
2431 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2437 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2438 ctdb_addr_to_str(&addr)));
2439 talloc_free(tmp_ctx);
2443 /* This is an optimisation. If this node is hosting the IP
2444 * then try to move it somewhere else without invoking a full
2445 * takeover run. We don't care if this doesn't work!
2447 if (ips->ips[i].pnn == options.pnn) {
2448 (void) try_moveip(ctdb, &addr, -1);
2451 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2453 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2454 talloc_free(tmp_ctx);
2458 talloc_free(tmp_ctx);
2462 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2463 int argc, const char **argv)
2465 struct ctdb_control_killtcp *killtcp;
2466 int max_entries, current, i;
2467 struct timeval timeout;
2468 char line[128], src[128], dst[128];
2471 struct client_async_data *async_data;
2472 struct ctdb_client_control_state *state;
2482 while (!feof(stdin)) {
2483 if (fgets(line, sizeof(line), stdin) == NULL) {
2487 /* Silently skip empty lines */
2488 if (line[0] == '\n') {
2492 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2493 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2495 talloc_free(killtcp);
2499 if (current >= max_entries) {
2500 max_entries += 1024;
2501 killtcp = talloc_realloc(ctdb, killtcp,
2502 struct ctdb_control_killtcp,
2504 CTDB_NO_MEMORY(ctdb, killtcp);
2507 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2508 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2510 talloc_free(killtcp);
2514 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2515 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2517 talloc_free(killtcp);
2524 async_data = talloc_zero(ctdb, struct client_async_data);
2525 if (async_data == NULL) {
2526 talloc_free(killtcp);
2530 for (i = 0; i < current; i++) {
2532 data.dsize = sizeof(struct ctdb_control_killtcp);
2533 data.dptr = (unsigned char *)&killtcp[i];
2535 timeout = TIMELIMIT();
2536 state = ctdb_control_send(ctdb, options.pnn, 0,
2537 CTDB_CONTROL_KILL_TCP, 0, data,
2538 async_data, &timeout, NULL);
2540 if (state == NULL) {
2542 ("Failed to call async killtcp control to node %u\n",
2544 talloc_free(killtcp);
2548 ctdb_client_async_add(async_data, state);
2551 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2552 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2553 talloc_free(killtcp);
2557 talloc_free(killtcp);
2563 kill a tcp connection
2565 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2568 struct ctdb_control_killtcp killtcp;
2570 assert_single_node_only();
2573 return kill_tcp_from_file(ctdb, argc, argv);
2580 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2581 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2585 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2586 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2590 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2592 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2603 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2606 ctdb_sock_addr addr;
2608 assert_single_node_only();
2614 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2615 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2619 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2621 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2629 register a server id
2631 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2634 struct ctdb_server_id server_id;
2640 server_id.pnn = strtoul(argv[0], NULL, 0);
2641 server_id.type = strtoul(argv[1], NULL, 0);
2642 server_id.server_id = strtoul(argv[2], NULL, 0);
2644 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2646 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2649 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2655 unregister a server id
2657 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2660 struct ctdb_server_id server_id;
2666 server_id.pnn = strtoul(argv[0], NULL, 0);
2667 server_id.type = strtoul(argv[1], NULL, 0);
2668 server_id.server_id = strtoul(argv[2], NULL, 0);
2670 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2672 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2679 check if a server id exists
2681 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2685 struct ctdb_server_id server_id;
2691 server_id.pnn = strtoul(argv[0], NULL, 0);
2692 server_id.type = strtoul(argv[1], NULL, 0);
2693 server_id.server_id = strtoul(argv[2], NULL, 0);
2695 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2697 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2702 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2704 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2710 get a list of all server ids that are registered on a node
2712 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2715 struct ctdb_server_id_list *server_ids;
2717 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2719 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2723 for (i=0; i<server_ids->num; i++) {
2724 printf("Server id %d:%d:%d\n",
2725 server_ids->server_ids[i].pnn,
2726 server_ids->server_ids[i].type,
2727 server_ids->server_ids[i].server_id);
2734 check if a server id exists
2736 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2738 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2744 talloc_free(tmp_ctx);
2748 ids = talloc_array(tmp_ctx, uint64_t, argc);
2749 result = talloc_array(tmp_ctx, uint8_t, argc);
2751 for (i = 0; i < argc; i++) {
2752 ids[i] = strtoull(argv[i], NULL, 0);
2755 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2756 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2758 talloc_free(tmp_ctx);
2762 for (i=0; i < argc; i++) {
2763 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2764 result[i] ? "exists" : "does not exist");
2767 talloc_free(tmp_ctx);
2772 send a tcp tickle ack
2774 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2777 ctdb_sock_addr src, dst;
2783 if (!parse_ip_port(argv[0], &src)) {
2784 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2788 if (!parse_ip_port(argv[1], &dst)) {
2789 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2793 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2797 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2804 display public ip status
2806 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2809 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2810 struct ctdb_all_public_ips *ips;
2812 if (options.pnn == CTDB_BROADCAST_ALL) {
2813 /* read the list of public ips from all nodes */
2814 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2816 /* read the public ip list from this node */
2817 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2820 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2821 talloc_free(tmp_ctx);
2825 if (options.machinereadable){
2826 printf(":Public IP:Node:");
2827 if (options.verbose){
2828 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2832 if (options.pnn == CTDB_BROADCAST_ALL) {
2833 printf("Public IPs on ALL nodes\n");
2835 printf("Public IPs on node %u\n", options.pnn);
2839 for (i=1;i<=ips->num;i++) {
2840 struct ctdb_control_public_ip_info *info = NULL;
2842 char *aciface = NULL;
2843 char *avifaces = NULL;
2844 char *cifaces = NULL;
2846 if (options.pnn == CTDB_BROADCAST_ALL) {
2847 pnn = ips->ips[ips->num-i].pnn;
2853 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2854 &ips->ips[ips->num-i].addr, &info);
2861 for (j=0; j < info->num; j++) {
2862 if (cifaces == NULL) {
2863 cifaces = talloc_strdup(info,
2864 info->ifaces[j].name);
2866 cifaces = talloc_asprintf_append(cifaces,
2868 info->ifaces[j].name);
2871 if (info->active_idx == j) {
2872 aciface = info->ifaces[j].name;
2875 if (info->ifaces[j].link_state == 0) {
2879 if (avifaces == NULL) {
2880 avifaces = talloc_strdup(info, info->ifaces[j].name);
2882 avifaces = talloc_asprintf_append(avifaces,
2884 info->ifaces[j].name);
2889 if (options.machinereadable){
2891 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2892 ips->ips[ips->num-i].pnn);
2893 if (options.verbose){
2896 avifaces?avifaces:"",
2897 cifaces?cifaces:"");
2901 if (options.verbose) {
2902 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2903 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2904 ips->ips[ips->num-i].pnn,
2906 avifaces?avifaces:"",
2907 cifaces?cifaces:"");
2910 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2911 ips->ips[ips->num-i].pnn);
2917 talloc_free(tmp_ctx);
2924 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2927 ctdb_sock_addr addr;
2928 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2929 struct ctdb_control_public_ip_info *info;
2932 talloc_free(tmp_ctx);
2936 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2937 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2941 /* read the public ip info from this node */
2942 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2943 tmp_ctx, &addr, &info);
2945 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2946 argv[0], options.pnn));
2947 talloc_free(tmp_ctx);
2951 printf("Public IP[%s] info on node %u\n",
2952 ctdb_addr_to_str(&info->ip.addr),
2955 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2956 ctdb_addr_to_str(&info->ip.addr),
2957 info->ip.pnn, info->num);
2959 for (i=0; i<info->num; i++) {
2960 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2962 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2963 i+1, info->ifaces[i].name,
2964 info->ifaces[i].link_state?"up":"down",
2965 (unsigned int)info->ifaces[i].references,
2966 (i==info->active_idx)?" (active)":"");
2969 talloc_free(tmp_ctx);
2974 display interfaces status
2976 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2978 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2980 struct ctdb_control_get_ifaces *ifaces;
2983 /* read the public ip list from this node */
2984 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
2986 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2988 talloc_free(tmp_ctx);
2992 if (options.machinereadable){
2993 printf(":Name:LinkStatus:References:\n");
2995 printf("Interfaces on node %u\n", options.pnn);
2998 for (i=0; i<ifaces->num; i++) {
2999 if (options.machinereadable){
3000 printf(":%s:%s:%u\n",
3001 ifaces->ifaces[i].name,
3002 ifaces->ifaces[i].link_state?"1":"0",
3003 (unsigned int)ifaces->ifaces[i].references);
3005 printf("name:%s link:%s references:%u\n",
3006 ifaces->ifaces[i].name,
3007 ifaces->ifaces[i].link_state?"up":"down",
3008 (unsigned int)ifaces->ifaces[i].references);
3012 talloc_free(tmp_ctx);
3018 set link status of an interface
3020 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3023 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3024 struct ctdb_control_iface_info info;
3032 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3033 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3035 talloc_free(tmp_ctx);
3038 strcpy(info.name, argv[0]);
3040 if (strcmp(argv[1], "up") == 0) {
3041 info.link_state = 1;
3042 } else if (strcmp(argv[1], "down") == 0) {
3043 info.link_state = 0;
3045 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3047 talloc_free(tmp_ctx);
3051 /* read the public ip list from this node */
3052 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3055 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3056 argv[0], options.pnn));
3057 talloc_free(tmp_ctx);
3061 talloc_free(tmp_ctx);
3066 display pid of a ctdb daemon
3068 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3073 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3075 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3078 printf("Pid:%d\n", pid);
3083 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3085 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3087 update_flags_handler_t handler,
3092 struct ctdb_node_map *nodemap = NULL;
3096 /* Check if the node is already in the desired state */
3097 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3099 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3102 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3103 if (set_flag == flag_is_set) {
3104 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3105 (set_flag ? "already" : "not"), desc));
3110 if (!handler(ctdb, data)) {
3111 DEBUG(DEBUG_WARNING,
3112 ("Failed to send control to set state %s on node %u, try again\n",
3113 desc, options.pnn));
3118 /* Read the nodemap and verify the change took effect.
3119 * Even if the above control/hanlder timed out then it
3120 * could still have worked!
3122 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3125 DEBUG(DEBUG_WARNING,
3126 ("Unable to get nodemap from local node, try again\n"));
3128 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3129 } while (nodemap == NULL || (set_flag != flag_is_set));
3131 return ipreallocate(ctdb);
3134 /* Administratively disable a node */
3135 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3139 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3140 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3144 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3146 return update_flags_and_ipreallocate(ctdb, NULL,
3147 update_flags_disabled,
3148 NODE_FLAGS_PERMANENTLY_DISABLED,
3150 true /* set_flag*/);
3153 /* Administratively re-enable a node */
3154 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3158 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3159 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3163 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3165 return update_flags_and_ipreallocate(ctdb, NULL,
3166 update_flags_not_disabled,
3167 NODE_FLAGS_PERMANENTLY_DISABLED,
3169 false /* set_flag*/);
3173 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3177 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3182 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3184 return update_flags_and_ipreallocate(ctdb, NULL,
3185 update_flags_stopped,
3188 true /* set_flag*/);
3191 /* Continue a stopped node */
3192 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3196 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3201 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3203 return update_flags_and_ipreallocate(ctdb, NULL,
3204 update_flags_not_stopped,
3207 false /* set_flag */);
3210 static uint32_t get_generation(struct ctdb_context *ctdb)
3212 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3213 struct ctdb_vnn_map *vnnmap=NULL;
3215 uint32_t generation;
3217 /* wait until the recmaster is not in recovery mode */
3219 uint32_t recmode, recmaster;
3221 if (vnnmap != NULL) {
3222 talloc_free(vnnmap);
3226 /* get the recmaster */
3227 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3229 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3230 talloc_free(tmp_ctx);
3234 /* get recovery mode */
3235 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3237 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3238 talloc_free(tmp_ctx);
3242 /* get the current generation number */
3243 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3245 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3246 talloc_free(tmp_ctx);
3250 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3251 generation = vnnmap->generation;
3252 talloc_free(tmp_ctx);
3260 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3262 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3265 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3270 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3272 struct ctdb_ban_time bantime;
3278 bantime.pnn = options.pnn;
3279 bantime.time = strtoul(argv[0], NULL, 0);
3281 if (bantime.time == 0) {
3282 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3286 return update_flags_and_ipreallocate(ctdb, &bantime,
3287 update_state_banned,
3290 true /* set_flag*/);
3295 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3297 struct ctdb_ban_time bantime;
3299 bantime.pnn = options.pnn;
3302 return update_flags_and_ipreallocate(ctdb, &bantime,
3303 update_state_banned,
3306 false /* set_flag*/);
3310 show ban information for a node
3312 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3315 struct ctdb_node_map *nodemap=NULL;
3316 struct ctdb_ban_time *bantime;
3318 /* verify the node exists */
3319 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3321 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3325 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3327 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3331 if (bantime->time == 0) {
3332 printf("Node %u is not banned\n", bantime->pnn);
3334 printf("Node %u is banned, %d seconds remaining\n",
3335 bantime->pnn, bantime->time);
3344 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3348 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3350 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3360 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3363 uint32_t generation, next_generation;
3366 /* "force" option ignores freeze failure and forces recovery */
3367 force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3369 /* record the current generation number */
3370 generation = get_generation(ctdb);
3372 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3375 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3378 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3381 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3383 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3387 /* wait until we are in a new generation */
3389 next_generation = get_generation(ctdb);
3390 if (next_generation != generation) {
3401 display monitoring mode of a remote node
3403 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3408 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3410 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3413 if (!options.machinereadable){
3414 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3417 printf(":%d:\n",monmode);
3424 display capabilities of a remote node
3426 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3428 uint32_t capabilities;
3431 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3433 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3437 if (!options.machinereadable){
3438 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3439 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3440 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3441 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3443 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3444 printf(":%d:%d:%d:%d:\n",
3445 !!(capabilities&CTDB_CAP_RECMASTER),
3446 !!(capabilities&CTDB_CAP_LMASTER),
3447 !!(capabilities&CTDB_CAP_LVS),
3448 !!(capabilities&CTDB_CAP_NATGW));
3454 display lvs configuration
3456 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3458 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3459 uint32_t *capabilities;
3460 struct ctdb_node_map *nodemap=NULL;
3462 int healthy_count = 0;
3464 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
3466 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3467 talloc_free(tmp_ctx);
3471 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3472 CTDB_NO_MEMORY(ctdb, capabilities);
3476 /* collect capabilities for all connected nodes */
3477 for (i=0; i<nodemap->num; i++) {
3478 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3481 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3485 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3487 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3492 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3496 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3501 /* Print all LVS nodes */
3502 for (i=0; i<nodemap->num; i++) {
3503 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3506 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3509 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3513 if (healthy_count != 0) {
3514 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3519 printf("%d:%s\n", i,
3520 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3524 talloc_free(tmp_ctx);
3529 display who is the lvs master
3531 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3533 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3534 uint32_t *capabilities;
3535 struct ctdb_node_map *nodemap=NULL;
3537 int healthy_count = 0;
3539 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
3541 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3542 talloc_free(tmp_ctx);
3546 capabilities = talloc_array(tmp_ctx, uint32_t, nodemap->num);
3547 if (capabilities == NULL) {
3548 talloc_free(tmp_ctx);
3549 CTDB_NO_MEMORY(ctdb, capabilities);
3552 /* collect capabilities for all connected nodes */
3553 for (i=0; i<nodemap->num; i++) {
3554 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3557 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3561 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3563 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3568 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3572 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3579 /* find and show the lvsmaster */
3580 for (i=0; i<nodemap->num; i++) {
3581 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3584 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3587 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3591 if (healthy_count != 0) {
3592 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3597 if (options.machinereadable){
3600 printf("Node %d is LVS master\n", i);
3606 printf("There is no LVS master\n");
3608 talloc_free(tmp_ctx);
3613 disable monitoring on a node
3615 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3620 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3622 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3625 printf("Monitoring mode:%s\n","DISABLED");
3631 enable monitoring on a node
3633 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3638 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3640 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3643 printf("Monitoring mode:%s\n","ACTIVE");
3649 display remote list of keys/data for a db
3651 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3653 const char *db_name;
3654 struct ctdb_db_context *ctdb_db;
3656 struct ctdb_dump_db_context c;
3665 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3669 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3670 if (ctdb_db == NULL) {
3671 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3675 if (options.printlmaster) {
3676 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3677 ctdb, &ctdb->vnn_map);
3679 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3687 c.printemptyrecords = (bool)options.printemptyrecords;
3688 c.printdatasize = (bool)options.printdatasize;
3689 c.printlmaster = (bool)options.printlmaster;
3690 c.printhash = (bool)options.printhash;
3691 c.printrecordflags = (bool)options.printrecordflags;
3693 /* traverse and dump the cluster tdb */
3694 ret = ctdb_dump_db(ctdb_db, &c);
3696 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3697 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3698 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3702 talloc_free(ctdb_db);
3704 printf("Dumped %d records\n", ret);
3708 struct cattdb_data {
3709 struct ctdb_context *ctdb;
3713 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3715 struct cattdb_data *d = private_data;
3716 struct ctdb_dump_db_context c;
3722 c.printemptyrecords = (bool)options.printemptyrecords;
3723 c.printdatasize = (bool)options.printdatasize;
3724 c.printlmaster = false;
3725 c.printhash = (bool)options.printhash;
3726 c.printrecordflags = true;
3728 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3732 cat the local tdb database using same format as catdb
3734 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3736 const char *db_name;
3737 struct ctdb_db_context *ctdb_db;
3738 struct cattdb_data d;
3747 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3751 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3752 if (ctdb_db == NULL) {
3753 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3757 /* traverse the local tdb */
3760 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3761 printf("Failed to cattdb data\n");
3764 talloc_free(ctdb_db);
3766 printf("Dumped %d records\n", d.count);
3771 display the content of a database key
3773 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3775 const char *db_name;
3776 struct ctdb_db_context *ctdb_db;
3777 struct ctdb_record_handle *h;
3778 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3788 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3792 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3793 if (ctdb_db == NULL) {
3794 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3798 key.dptr = discard_const(argv[1]);
3799 key.dsize = strlen((char *)key.dptr);
3801 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3803 printf("Failed to fetch record '%s' on node %d\n",
3804 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3805 talloc_free(tmp_ctx);
3809 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3811 talloc_free(ctdb_db);
3812 talloc_free(tmp_ctx);
3817 display the content of a database key
3819 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3821 const char *db_name;
3822 struct ctdb_db_context *ctdb_db;
3823 struct ctdb_record_handle *h;
3824 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3834 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3838 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3839 if (ctdb_db == NULL) {
3840 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3844 key.dptr = discard_const(argv[1]);
3845 key.dsize = strlen((char *)key.dptr);
3847 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3849 printf("Failed to fetch record '%s' on node %d\n",
3850 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3851 talloc_free(tmp_ctx);
3855 data.dptr = discard_const(argv[2]);
3856 data.dsize = strlen((char *)data.dptr);
3858 if (ctdb_record_store(h, data) != 0) {
3859 printf("Failed to store record\n");
3863 talloc_free(ctdb_db);
3864 talloc_free(tmp_ctx);
3869 fetch a record from a persistent database
3871 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3873 const char *db_name;
3874 struct ctdb_db_context *ctdb_db;
3875 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3876 struct ctdb_transaction_handle *h;
3883 talloc_free(tmp_ctx);
3889 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3890 talloc_free(tmp_ctx);
3894 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3896 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3897 talloc_free(tmp_ctx);
3901 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3902 if (ctdb_db == NULL) {
3903 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3904 talloc_free(tmp_ctx);
3908 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3910 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3911 talloc_free(tmp_ctx);
3915 key.dptr = discard_const(argv[1]);
3916 key.dsize = strlen(argv[1]);
3917 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3919 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3920 talloc_free(tmp_ctx);
3924 if (data.dsize == 0 || data.dptr == NULL) {
3925 DEBUG(DEBUG_ERR,("Record is empty\n"));
3926 talloc_free(tmp_ctx);
3931 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3933 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3934 talloc_free(tmp_ctx);
3937 write(fd, data.dptr, data.dsize);
3940 write(1, data.dptr, data.dsize);
3943 /* abort the transaction */
3947 talloc_free(tmp_ctx);
3952 fetch a record from a tdb-file
3954 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3956 const char *tdb_file;
3959 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3968 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3970 printf("Failed to open TDB file %s\n", tdb_file);
3974 if (!strncmp(argv[1], "0x", 2)) {
3975 key = hextodata(tmp_ctx, argv[1] + 2);
3976 if (key.dsize == 0) {
3977 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3981 key.dptr = discard_const(argv[1]);
3982 key.dsize = strlen(argv[1]);
3985 data = tdb_fetch(tdb, key);
3986 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3987 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3995 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3997 printf("Failed to open output file %s\n", argv[2]);
4000 if (options.verbose){
4001 write(fd, data.dptr, data.dsize);
4003 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4007 if (options.verbose){
4008 write(1, data.dptr, data.dsize);
4010 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4014 talloc_free(tmp_ctx);
4019 store a record and header to a tdb-file
4021 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4023 const char *tdb_file;
4026 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4034 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4036 printf("Failed to open TDB file %s\n", tdb_file);
4040 if (!strncmp(argv[1], "0x", 2)) {
4041 key = hextodata(tmp_ctx, argv[1] + 2);
4042 if (key.dsize == 0) {
4043 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4047 key.dptr = discard_const(argv[1]);
4048 key.dsize = strlen(argv[1]);
4051 if (!strncmp(argv[2], "0x", 2)) {
4052 data = hextodata(tmp_ctx, argv[2] + 2);
4053 if (data.dsize == 0) {
4054 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4058 data.dptr = discard_const(argv[2]);
4059 data.dsize = strlen(argv[2]);
4062 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
4063 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
4066 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4067 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4074 talloc_free(tmp_ctx);
4079 write a record to a persistent database
4081 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4083 const char *db_name;
4084 struct ctdb_db_context *ctdb_db;
4085 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4086 struct ctdb_transaction_handle *h;
4092 talloc_free(tmp_ctx);
4096 fd = open(argv[2], O_RDONLY);
4098 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4099 talloc_free(tmp_ctx);
4103 ret = fstat(fd, &st);
4105 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4107 talloc_free(tmp_ctx);
4111 if (!S_ISREG(st.st_mode)) {
4112 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4114 talloc_free(tmp_ctx);
4118 data.dsize = st.st_size;
4119 if (data.dsize == 0) {
4122 data.dptr = talloc_size(tmp_ctx, data.dsize);
4123 if (data.dptr == NULL) {
4124 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4126 talloc_free(tmp_ctx);
4129 ret = read(fd, data.dptr, data.dsize);
4130 if (ret != data.dsize) {
4131 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4133 talloc_free(tmp_ctx);
4142 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4143 if (ctdb_db == NULL) {
4144 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4145 talloc_free(tmp_ctx);
4149 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4151 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4152 talloc_free(tmp_ctx);
4156 key.dptr = discard_const(argv[1]);
4157 key.dsize = strlen(argv[1]);
4158 ret = ctdb_transaction_store(h, key, data);
4160 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4161 talloc_free(tmp_ctx);
4165 ret = ctdb_transaction_commit(h);
4167 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4168 talloc_free(tmp_ctx);
4173 talloc_free(tmp_ctx);
4178 * delete a record from a persistent database
4180 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4182 const char *db_name;
4183 struct ctdb_db_context *ctdb_db;
4184 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4185 struct ctdb_transaction_handle *h;
4192 talloc_free(tmp_ctx);
4198 if (!db_exists(ctdb, db_name, NULL, &flags)) {
4199 talloc_free(tmp_ctx);
4203 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4205 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4206 talloc_free(tmp_ctx);
4210 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4211 if (ctdb_db == NULL) {
4212 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4213 talloc_free(tmp_ctx);
4217 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4219 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4220 talloc_free(tmp_ctx);
4224 key.dptr = discard_const(argv[1]);
4225 key.dsize = strlen(argv[1]);
4226 ret = ctdb_transaction_store(h, key, tdb_null);
4228 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4229 talloc_free(tmp_ctx);
4233 ret = ctdb_transaction_commit(h);
4235 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4236 talloc_free(tmp_ctx);
4240 talloc_free(tmp_ctx);
4245 check if a service is bound to a port or not
4247 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4252 struct sockaddr_in sin;
4255 printf("Use: ctdb chktcport <port>\n");
4259 port = atoi(argv[0]);
4261 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4263 printf("Failed to open local socket\n");
4267 v = fcntl(s, F_GETFL, 0);
4268 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4269 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4272 bzero(&sin, sizeof(sin));
4273 sin.sin_family = PF_INET;
4274 sin.sin_port = htons(port);
4275 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4278 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4287 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4288 TDB_DATA data, void *private_data)
4290 DEBUG(DEBUG_ERR,("Log data received\n"));
4291 if (data.dsize > 0) {
4292 printf("%s", data.dptr);
4299 display a list of log messages from the in memory ringbuffer
4301 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4305 struct ctdb_get_log_addr log_addr;
4309 /* Process options */
4311 log_addr.pnn = ctdb_get_pnn(ctdb);
4312 log_addr.level = DEBUG_NOTICE;
4313 for (i = 0; i < argc; i++) {
4314 if (strcmp(argv[i], "recoverd") == 0) {
4315 main_daemon = false;
4317 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4318 log_addr.level = get_debug_by_desc(argv[i]);
4320 log_addr.level = strtol(argv[i], NULL, 0);
4325 /* Our message port is our PID */
4326 log_addr.srvid = getpid();
4328 data.dptr = (unsigned char *)&log_addr;
4329 data.dsize = sizeof(log_addr);
4331 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4333 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4336 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4341 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4343 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4344 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4345 if (ret != 0 || res != 0) {
4346 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4347 talloc_free(tmp_ctx);
4350 talloc_free(tmp_ctx);
4352 ret = ctdb_client_send_message(ctdb, options.pnn,
4353 CTDB_SRVID_GETLOG, data);
4355 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4360 tv = timeval_current();
4361 /* this loop will terminate when we have received the reply */
4362 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4363 event_loop_once(ctdb->ev);
4366 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4372 clear the in memory log area
4374 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4378 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4379 /* "recoverd" not given - get logs from main daemon */
4382 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4384 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4385 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4386 if (ret != 0 || res != 0) {
4387 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4388 talloc_free(tmp_ctx);
4392 talloc_free(tmp_ctx);
4394 TDB_DATA data; /* unused in recoverd... */
4397 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4399 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4407 /* Reload public IPs on a specified nodes */
4408 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4410 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4415 assert_single_node_only();
4421 /* Determine the nodes where IPs need to be reloaded */
4422 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4423 options.pnn, true, &nodes, &pnn_mode)) {
4429 /* Disable takeover runs on all connected nodes. A reply
4430 * indicating success is needed from each node so all nodes
4431 * will need to be active. This will retry until maxruntime
4432 * is exceeded, hence no error handling.
4434 * A check could be added to not allow reloading of IPs when
4435 * there are disconnected nodes. However, this should
4436 * probably be left up to the administrator.
4438 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, LONGTIMEOUT,
4439 "Disable takeover runs", true);
4441 /* Now tell all the desired nodes to reload their public IPs.
4442 * Keep trying this until it succeeds. This assumes all
4443 * failures are transient, which might not be true...
4445 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4446 nodes, 0, LONGTIMELIMIT(),
4448 NULL, NULL, NULL) != 0) {
4450 ("Unable to reload IPs on some nodes, try again.\n"));
4454 /* It isn't strictly necessary to wait until takeover runs are
4455 * re-enabled but doing so can't hurt.
4457 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, 0,
4458 "Enable takeover runs", true);
4464 talloc_free(tmp_ctx);
4469 display a list of the databases on a remote ctdb
4471 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4474 struct ctdb_dbid_map *dbmap=NULL;
4476 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4478 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4482 if(options.machinereadable){
4483 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4484 for(i=0;i<dbmap->num;i++){
4492 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4493 dbmap->dbs[i].dbid, ctdb, &path);
4494 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4495 dbmap->dbs[i].dbid, ctdb, &name);
4496 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4497 dbmap->dbs[i].dbid, ctdb, &health);
4498 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4499 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4500 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4501 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4502 dbmap->dbs[i].dbid, name, path,
4503 !!(persistent), !!(sticky),
4504 !!(health), !!(readonly));
4509 printf("Number of databases:%d\n", dbmap->num);
4510 for(i=0;i<dbmap->num;i++){
4518 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4519 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4520 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4521 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4522 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4523 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4524 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4525 dbmap->dbs[i].dbid, name, path,
4526 persistent?" PERSISTENT":"",
4527 sticky?" STICKY":"",
4528 readonly?" READONLY":"",
4529 health?" UNHEALTHY":"");
4536 display the status of a database on a remote ctdb
4538 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4540 const char *db_name;
4552 if (!db_exists(ctdb, db_name, &db_id, &flags)) {
4556 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4557 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4558 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4559 db_id, db_name, path,
4560 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4561 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4562 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4563 (health ? health : "OK"));
4569 check if the local node is recmaster or not
4570 it will return 1 if this node is the recmaster and 0 if it is not
4571 or if the local ctdb daemon could not be contacted
4573 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4575 uint32_t mypnn, recmaster;
4578 assert_single_node_only();
4580 mypnn = getpnn(ctdb);
4582 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4584 printf("Failed to get the recmaster\n");
4588 if (recmaster != mypnn) {
4589 printf("this node is not the recmaster\n");
4593 printf("this node is the recmaster\n");
4600 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4603 struct timeval tv = timeval_current();
4604 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4606 printf("Unable to get ping response from node %u\n", options.pnn);
4609 printf("response from %u time=%.6f sec (%d clients)\n",
4610 options.pnn, timeval_elapsed(&tv), ret);
4617 get a node's runstate
4619 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4622 enum ctdb_runstate runstate;
4624 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4626 printf("Unable to get runstate response from node %u\n",
4631 enum ctdb_runstate t;
4633 for (i=0; i<argc; i++) {
4635 t = runstate_from_string(argv[i]);
4636 if (t == CTDB_RUNSTATE_UNKNOWN) {
4637 printf("Invalid run state (%s)\n", argv[i]);
4641 if (t == runstate) {
4648 printf("CTDB not in required run state (got %s)\n",
4649 runstate_to_string((enum ctdb_runstate)runstate));
4654 printf("%s\n", runstate_to_string(runstate));
4662 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4673 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4675 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4679 printf("%-23s = %u\n", name, value);
4686 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4697 value = strtoul(argv[1], NULL, 0);
4699 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4701 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4710 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4716 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4718 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4722 for (i=0;i<count;i++) {
4723 control_getvar(ctdb, 1, &list[i]);
4732 display debug level on a node
4734 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4739 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4741 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4744 if (options.machinereadable){
4745 printf(":Name:Level:\n");
4746 printf(":%s:%d:\n",get_debug_by_level(level),level);
4748 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4755 display reclock file of a node
4757 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4760 const char *reclock;
4762 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4764 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4767 if (options.machinereadable){
4768 if (reclock != NULL) {
4769 printf("%s", reclock);
4772 if (reclock == NULL) {
4773 printf("No reclock file used.\n");
4775 printf("Reclock file:%s\n", reclock);
4783 set the reclock file of a node
4785 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4788 const char *reclock;
4792 } else if (argc == 1) {
4798 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4800 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4807 set the natgw state on/off
4809 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4812 uint32_t natgwstate;
4818 if (!strcmp(argv[0], "on")) {
4820 } else if (!strcmp(argv[0], "off")) {
4826 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4828 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4836 set the lmaster role on/off
4838 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4841 uint32_t lmasterrole;
4847 if (!strcmp(argv[0], "on")) {
4849 } else if (!strcmp(argv[0], "off")) {
4855 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4857 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4865 set the recmaster role on/off
4867 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4870 uint32_t recmasterrole;
4876 if (!strcmp(argv[0], "on")) {
4878 } else if (!strcmp(argv[0], "off")) {
4884 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4886 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4894 set debug level on a node or all nodes
4896 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4902 printf("You must specify the debug level. Valid levels are:\n");
4903 for (i=0; debug_levels[i].description != NULL; i++) {
4904 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4910 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4911 level = get_debug_by_desc(argv[0]);
4913 level = strtol(argv[0], NULL, 0);
4916 for (i=0; debug_levels[i].description != NULL; i++) {
4917 if (level == debug_levels[i].level) {
4921 if (debug_levels[i].description == NULL) {
4922 printf("Invalid debug level, must be one of\n");
4923 for (i=0; debug_levels[i].description != NULL; i++) {
4924 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4929 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4931 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4940 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4946 priority = strtol(argv[0], NULL, 0);
4950 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4952 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4954 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4961 attach to a database
4963 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4965 const char *db_name;
4966 struct ctdb_db_context *ctdb_db;
4967 bool persistent = false;
4977 if (strcmp(argv[1], "persistent") != 0) {
4983 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4984 if (ctdb_db == NULL) {
4985 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4995 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4997 struct ctdb_db_priority db_prio;
5004 db_prio.db_id = strtoul(argv[0], NULL, 0);
5005 db_prio.priority = strtoul(argv[1], NULL, 0);
5007 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5009 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5019 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5021 uint32_t db_id, priority;
5028 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5032 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5034 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5038 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5044 set the sticky records capability for a database
5046 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5048 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5056 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5060 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5062 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5063 talloc_free(tmp_ctx);
5067 talloc_free(tmp_ctx);
5072 set the readonly capability for a database
5074 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5076 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5084 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5088 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5090 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5091 talloc_free(tmp_ctx);
5095 talloc_free(tmp_ctx);
5102 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5112 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5116 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5118 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5122 printf("Sequence number:%lld\n", (long long)seqnum);
5128 run an eventscript on a node
5130 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5136 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5139 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5143 data.dptr = (unsigned char *)discard_const(argv[0]);
5144 data.dsize = strlen((char *)data.dptr) + 1;
5146 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5148 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5149 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5150 if (ret != 0 || res != 0) {
5151 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5152 talloc_free(tmp_ctx);
5155 talloc_free(tmp_ctx);
5159 #define DB_VERSION 1
5160 #define MAX_DB_NAME 64
5161 struct db_file_header {
5162 unsigned long version;
5164 unsigned long persistent;
5166 const char name[MAX_DB_NAME];
5169 struct backup_data {
5170 struct ctdb_marshall_buffer *records;
5173 bool traverse_error;
5176 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5178 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5179 struct ctdb_rec_data *rec;
5181 /* add the record */
5182 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5184 bd->traverse_error = true;
5185 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5188 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5189 if (bd->records == NULL) {
5190 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5191 bd->traverse_error = true;
5194 bd->records->count++;
5195 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5196 bd->len += rec->length;
5204 * backup a database to a file
5206 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5209 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5210 struct db_file_header dbhdr;
5211 struct ctdb_db_context *ctdb_db;
5212 struct backup_data *bd;
5215 const char *reason = NULL;
5219 assert_single_node_only();
5222 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5226 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5230 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5231 db_id, tmp_ctx, &reason);
5233 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5235 talloc_free(tmp_ctx);
5239 uint32_t allow_unhealthy = 0;
5241 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5242 "AllowUnhealthyDBRead",
5245 if (allow_unhealthy != 1) {
5246 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5249 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5251 talloc_free(tmp_ctx);
5255 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5257 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5258 "tunnable AllowUnhealthyDBRead = %u\n",
5262 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5263 if (ctdb_db == NULL) {
5264 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5265 talloc_free(tmp_ctx);
5270 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5272 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5273 talloc_free(tmp_ctx);
5278 bd = talloc_zero(tmp_ctx, struct backup_data);
5280 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5281 talloc_free(tmp_ctx);
5285 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5286 if (bd->records == NULL) {
5287 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5288 talloc_free(tmp_ctx);
5292 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5293 bd->records->db_id = ctdb_db->db_id;
5294 /* traverse the database collecting all records */
5295 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5296 bd->traverse_error) {
5297 DEBUG(DEBUG_ERR,("Traverse error\n"));
5298 talloc_free(tmp_ctx);
5302 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5305 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5307 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5308 talloc_free(tmp_ctx);
5313 dbhdr.version = DB_VERSION;
5314 dbhdr.timestamp = time(NULL);
5315 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5316 dbhdr.size = bd->len;
5317 if (strlen(argv[0]) >= MAX_DB_NAME) {
5318 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5321 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5322 ret = write(fh, &dbhdr, sizeof(dbhdr));
5324 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5327 ret = write(fh, bd->records, bd->len);
5329 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5338 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5342 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5344 talloc_free(tmp_ctx);
5349 * restore a database from a file
5351 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5354 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5357 struct db_file_header dbhdr;
5358 struct ctdb_db_context *ctdb_db;
5359 struct ctdb_node_map *nodemap=NULL;
5360 struct ctdb_vnn_map *vnnmap=NULL;
5362 struct ctdb_control_wipe_database w;
5364 uint32_t generation;
5369 assert_single_node_only();
5371 if (argc < 1 || argc > 2) {
5372 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5376 fh = open(argv[0], O_RDONLY);
5378 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5379 talloc_free(tmp_ctx);
5383 read(fh, &dbhdr, sizeof(dbhdr));
5384 if (dbhdr.version != DB_VERSION) {
5385 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5387 talloc_free(tmp_ctx);
5391 dbname = discard_const(dbhdr.name);
5393 dbname = discard_const(argv[1]);
5396 outdata.dsize = dbhdr.size;
5397 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5398 if (outdata.dptr == NULL) {
5399 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5401 talloc_free(tmp_ctx);
5404 read(fh, outdata.dptr, outdata.dsize);
5407 tm = localtime(&dbhdr.timestamp);
5408 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5409 printf("Restoring database '%s' from backup @ %s\n",
5413 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5414 if (ctdb_db == NULL) {
5415 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5416 talloc_free(tmp_ctx);
5420 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5422 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5423 talloc_free(tmp_ctx);
5428 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5430 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5431 talloc_free(tmp_ctx);
5435 /* freeze all nodes */
5436 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5437 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5438 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5444 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5445 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5446 talloc_free(tmp_ctx);
5451 generation = vnnmap->generation;
5452 data.dptr = (void *)&generation;
5453 data.dsize = sizeof(generation);
5455 /* start a cluster wide transaction */
5456 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5457 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5459 TIMELIMIT(), false, data,
5462 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5467 w.db_id = ctdb_db->db_id;
5468 w.transaction_id = generation;
5470 data.dptr = (void *)&w;
5471 data.dsize = sizeof(w);
5473 /* wipe all the remote databases. */
5474 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5475 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5477 TIMELIMIT(), false, data,
5480 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5481 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5482 talloc_free(tmp_ctx);
5486 /* push the database */
5487 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5488 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5490 TIMELIMIT(), false, outdata,
5493 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5494 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5495 talloc_free(tmp_ctx);
5499 data.dptr = (void *)&ctdb_db->db_id;
5500 data.dsize = sizeof(ctdb_db->db_id);
5502 /* mark the database as healthy */
5503 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5504 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5506 TIMELIMIT(), false, data,
5509 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5510 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5511 talloc_free(tmp_ctx);
5515 data.dptr = (void *)&generation;
5516 data.dsize = sizeof(generation);
5518 /* commit all the changes */
5519 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5521 TIMELIMIT(), false, data,
5524 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5525 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5526 talloc_free(tmp_ctx);
5531 /* thaw all nodes */
5532 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5533 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5539 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5540 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5541 talloc_free(tmp_ctx);
5546 talloc_free(tmp_ctx);
5551 * dump a database backup from a file
5553 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5555 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5557 struct db_file_header dbhdr;
5561 struct ctdb_rec_data *rec = NULL;
5562 struct ctdb_marshall_buffer *m;
5563 struct ctdb_dump_db_context c;
5565 assert_single_node_only();
5568 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5572 fh = open(argv[0], O_RDONLY);
5574 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5575 talloc_free(tmp_ctx);
5579 read(fh, &dbhdr, sizeof(dbhdr));
5580 if (dbhdr.version != DB_VERSION) {
5581 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5583 talloc_free(tmp_ctx);
5587 outdata.dsize = dbhdr.size;
5588 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5589 if (outdata.dptr == NULL) {
5590 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5592 talloc_free(tmp_ctx);
5595 read(fh, outdata.dptr, outdata.dsize);
5597 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5599 tm = localtime(&dbhdr.timestamp);
5600 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5601 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5602 dbhdr.name, m->db_id, tbuf);
5606 c.printemptyrecords = (bool)options.printemptyrecords;
5607 c.printdatasize = (bool)options.printdatasize;
5608 c.printlmaster = false;
5609 c.printhash = (bool)options.printhash;
5610 c.printrecordflags = (bool)options.printrecordflags;
5612 for (i=0; i < m->count; i++) {
5616 /* we do not want the header splitted, so we pass NULL*/
5617 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5620 ctdb_dumpdb_record(ctdb, key, data, &c);
5623 printf("Dumped %d records\n", i);
5624 talloc_free(tmp_ctx);
5629 * wipe a database from a file
5631 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5635 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5637 struct ctdb_db_context *ctdb_db;
5638 struct ctdb_node_map *nodemap = NULL;
5639 struct ctdb_vnn_map *vnnmap = NULL;
5641 struct ctdb_control_wipe_database w;
5643 uint32_t generation;
5646 assert_single_node_only();
5649 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5653 if (!db_exists(ctdb, argv[0], NULL, &flags)) {
5657 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5658 if (ctdb_db == NULL) {
5659 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5661 talloc_free(tmp_ctx);
5665 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5668 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5670 talloc_free(tmp_ctx);
5674 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5677 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5679 talloc_free(tmp_ctx);
5683 /* freeze all nodes */
5684 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5685 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5686 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5693 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5694 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5695 CTDB_RECOVERY_ACTIVE);
5696 talloc_free(tmp_ctx);
5701 generation = vnnmap->generation;
5702 data.dptr = (void *)&generation;
5703 data.dsize = sizeof(generation);
5705 /* start a cluster wide transaction */
5706 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5707 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5709 TIMELIMIT(), false, data,
5713 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5714 "transactions.\n"));
5718 w.db_id = ctdb_db->db_id;
5719 w.transaction_id = generation;
5721 data.dptr = (void *)&w;
5722 data.dsize = sizeof(w);
5724 /* wipe all the remote databases. */
5725 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5726 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5728 TIMELIMIT(), false, data,
5731 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5732 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5733 talloc_free(tmp_ctx);
5737 data.dptr = (void *)&ctdb_db->db_id;
5738 data.dsize = sizeof(ctdb_db->db_id);
5740 /* mark the database as healthy */
5741 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5742 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5744 TIMELIMIT(), false, data,
5747 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5748 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5749 talloc_free(tmp_ctx);
5753 data.dptr = (void *)&generation;
5754 data.dsize = sizeof(generation);
5756 /* commit all the changes */
5757 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5759 TIMELIMIT(), false, data,
5762 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5763 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5764 talloc_free(tmp_ctx);
5768 /* thaw all nodes */
5769 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5770 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5776 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5777 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5778 talloc_free(tmp_ctx);
5782 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5784 talloc_free(tmp_ctx);
5791 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5797 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5798 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5799 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5800 if (ret != 0 || res != 0) {
5801 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5802 talloc_free(tmp_ctx);
5805 write(1, data.dptr, data.dsize);
5806 talloc_free(tmp_ctx);
5811 handler for memory dumps
5813 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
5814 TDB_DATA data, void *private_data)
5816 write(1, data.dptr, data.dsize);
5821 dump memory usage on the recovery daemon
5823 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5827 struct srvid_request rd;
5829 rd.pnn = ctdb_get_pnn(ctdb);
5830 rd.srvid = getpid();
5832 /* register a message port for receiveing the reply so that we
5833 can receive the reply
5835 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5838 data.dptr = (uint8_t *)&rd;
5839 data.dsize = sizeof(rd);
5841 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5843 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5847 /* this loop will terminate when we have received the reply */
5849 event_loop_once(ctdb->ev);
5856 send a message to a srvid
5858 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5860 unsigned long srvid;
5868 srvid = strtoul(argv[0], NULL, 0);
5870 data.dptr = (uint8_t *)discard_const(argv[1]);
5871 data.dsize= strlen(argv[1]);
5873 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5875 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5883 handler for msglisten
5885 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5886 TDB_DATA data, void *private_data)
5890 printf("Message received: ");
5891 for (i=0;i<data.dsize;i++) {
5892 printf("%c", data.dptr[i]);
5898 listen for messages on a messageport
5900 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5906 /* register a message port and listen for messages
5908 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5909 printf("Listening for messages on srvid:%d\n", (int)srvid);
5912 event_loop_once(ctdb->ev);
5919 list all nodes in the cluster
5920 we parse the nodes file directly
5922 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5924 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5925 struct pnn_node *pnn_nodes;
5926 struct pnn_node *pnn_node;
5928 assert_single_node_only();
5930 pnn_nodes = read_nodes_file(mem_ctx);
5931 if (pnn_nodes == NULL) {
5932 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5933 talloc_free(mem_ctx);
5937 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5938 ctdb_sock_addr addr;
5939 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5940 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5941 talloc_free(mem_ctx);
5944 if (options.machinereadable){
5945 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5947 printf("%s\n", pnn_node->addr);
5950 talloc_free(mem_ctx);
5956 reload the nodes file on the local node
5958 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5962 struct ctdb_node_map *nodemap=NULL;
5964 assert_single_node_only();
5966 mypnn = ctdb_get_pnn(ctdb);
5968 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5970 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5974 /* reload the nodes file on all remote nodes */
5975 for (i=0;i<nodemap->num;i++) {
5976 if (nodemap->nodes[i].pnn == mypnn) {
5979 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5980 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5981 nodemap->nodes[i].pnn);
5983 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5987 /* reload the nodes file on the local node */
5988 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5989 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5991 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5994 /* initiate a recovery */
5995 control_recover(ctdb, argc, argv);
6001 static const struct {
6003 int (*fn)(struct ctdb_context *, int, const char **);
6005 bool without_daemon; /* can be run without daemon running ? */
6008 } ctdb_commands[] = {
6009 { "version", control_version, true, true, "show version of ctdb" },
6010 { "status", control_status, true, false, "show node status" },
6011 { "uptime", control_uptime, true, false, "show node uptime" },
6012 { "ping", control_ping, true, false, "ping all nodes" },
6013 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6014 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6015 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6016 { "listvars", control_listvars, true, false, "list tunable variables"},
6017 { "statistics", control_statistics, false, false, "show statistics" },
6018 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6019 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6020 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6021 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6022 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6023 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6024 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6025 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6026 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6027 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6028 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6029 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6030 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6031 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6032 { "lvs", control_lvs, true, false, "show lvs configuration" },
6033 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6034 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6035 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6036 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6037 { "getdebug", control_getdebug, true, false, "get debug level" },
6038 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6039 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6040 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6041 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6042 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6043 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6044 { "disable", control_disable, true, false, "disable a nodes public IP" },
6045 { "enable", control_enable, true, false, "enable a nodes public IP" },
6046 { "stop", control_stop, true, false, "stop a node" },
6047 { "continue", control_continue, true, false, "re-start a stopped node" },
6048 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6049 { "unban", control_unban, true, false, "unban a node" },
6050 { "showban", control_showban, true, false, "show ban information"},
6051 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6052 { "recover", control_recover, true, false, "force recovery" },
6053 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6054 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6055 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6056 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6057 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6058 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6059 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6060 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6061 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6063 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6065 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6066 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6067 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6068 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6069 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6070 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6071 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6072 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6073 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6074 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6075 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6076 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6077 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6078 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6079 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6080 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6081 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6082 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6083 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6084 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6085 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6086 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6087 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6088 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6089 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6090 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6091 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6092 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6093 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6094 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6095 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6096 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6097 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6098 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6099 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6100 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6101 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6102 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6103 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
6104 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
6105 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
6106 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6107 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6108 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6109 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6110 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6111 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6112 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6118 static void usage(void)
6122 "Usage: ctdb [options] <control>\n" \
6124 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6125 " -Y generate machinereadable output\n"
6126 " -v generate verbose output\n"
6127 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6128 printf("Controls:\n");
6129 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6130 printf(" %-15s %-27s %s\n",
6131 ctdb_commands[i].name,
6132 ctdb_commands[i].args?ctdb_commands[i].args:"",
6133 ctdb_commands[i].msg);
6139 static void ctdb_alarm(int sig)
6141 printf("Maximum runtime exceeded - exiting\n");
6148 int main(int argc, const char *argv[])
6150 struct ctdb_context *ctdb;
6151 char *nodestring = NULL;
6152 struct poptOption popt_options[] = {
6155 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6156 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6157 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6158 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6159 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6160 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6161 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6162 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6163 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6164 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6168 const char **extra_argv;
6172 struct event_context *ev;
6173 const char *control;
6177 /* set some defaults */
6178 options.maxruntime = 0;
6179 options.timelimit = 10;
6180 options.pnn = CTDB_CURRENT_NODE;
6182 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6184 while ((opt = poptGetNextOpt(pc)) != -1) {
6187 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6188 poptBadOption(pc, 0), poptStrerror(opt)));
6193 /* setup the remaining options for the main program to use */
6194 extra_argv = poptGetArgs(pc);
6197 while (extra_argv[extra_argc]) extra_argc++;
6200 if (extra_argc < 1) {
6204 if (options.maxruntime == 0) {
6205 const char *ctdb_timeout;
6206 ctdb_timeout = getenv("CTDB_TIMEOUT");
6207 if (ctdb_timeout != NULL) {
6208 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6210 /* default timeout is 120 seconds */
6211 options.maxruntime = 120;
6215 signal(SIGALRM, ctdb_alarm);
6216 alarm(options.maxruntime);
6218 control = extra_argv[0];
6220 /* Default value for CTDB_BASE - don't override */
6221 setenv("CTDB_BASE", ETCDIR "/ctdb", 0);
6223 ev = event_context_init(NULL);
6225 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6229 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6230 if (strcmp(control, ctdb_commands[i].name) == 0) {
6235 if (i == ARRAY_SIZE(ctdb_commands)) {
6236 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6240 if (ctdb_commands[i].without_daemon == true) {
6241 if (nodestring != NULL) {
6242 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6246 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6249 /* initialise ctdb */
6250 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6253 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6257 /* setup the node number(s) to contact */
6258 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6259 &options.nodes, &options.pnn)) {
6263 if (options.pnn == CTDB_CURRENT_NODE) {
6264 options.pnn = options.nodes[0];
6267 if (ctdb_commands[i].auto_all &&
6268 ((options.pnn == CTDB_BROADCAST_ALL) ||
6269 (options.pnn == CTDB_MULTICAST))) {
6273 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6274 options.pnn = options.nodes[j];
6275 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6278 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6283 (void)poptFreeContext(pc);